spring-framework: `BeanCurrentlyInCreationException` error depends on `@ComponentScan` `basePackages` values order [SPR-14307]
István pató opened SPR-14307 and commented
https://github.com/spring-projects/spring-boot/issues/6045
We have an issue with ‘circular reference’ error. When I started my Spring Boot app on my Ubuntu 16.04 desktop OS, the issue is not showing up. If I running the app on the Ubuntu 16.10 Server OS, then I get an error:
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘exampleService’: Requested bean is currently in creation: Is there an unresolvable circular reference?
If I change
@ComponentScan(basePackages = "org.springframework.boot.issues.ghXXXX")
to
@ComponentScan(basePackages = {"org.springframework.boot.issues.ghXXXX.server", "org.springframework.boot.issues.ghXXXX.service", "org.springframework.boot.issues.ghXXXX"})
then I don’t get BeanCurrentlyInCreationException on Ubuntu 16.10 Server OS.
I can reproduce the issue with a maven project. The code is failing on same OS, if I change @ComponentScan basePackages order.
Affects: 4.2.6
Reference URL: https://github.com/patoi/spring-boot-issues/tree/gh-6045/gh-6045
3 votes, 6 watchers
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 6
- Comments: 29 (4 by maintainers)
I’m afraid that this is still a reality.
I had that problem and I was fighting with it for the past 3 days. In my case it was something more weird. I was developing and running from IntelliJ. Everything was fine there. When I deployed the software to another machine resembling the production environment, I got a circular dependency error. After lots of trial and error I realized what the difference was: intelliJ relies on the class files to run the application. We were making a jar. After I set IntelliJ to create a jar and run the jar on my machine I got the same error.
I was able to fix it with the workaround mentioned here. There is still the concern though why this thing happens. I enabled debugging for spring, and I saw that the
Eagerly caching bean 'XYZ' to allow for resolving potential circular referencesmessages were not appearing with the same order (comparing the classes vs jar execution). I also saw 45 less messages (compared to 144 that I was expecting to see). Again the only difference is the classes vs jar. The code is the same. This puzzles me as there shouldn’t(?) be any differences between the 2 execution methods.So the questions are:
I am at your disposal to provide any additional information as my team is very keen on having that resolved. The “random” manner of bean processing makes neither my teammates nor our managers confident. 😦
I also encountered the same problem. In order to find the final cause, I traced the source code of springboot. I am glad that I finally found the root cause of the problem. The reason is that the order of instantiation of springboot beans is related to the order of the list of files returned from the jar package. The different platforms of the jar package lead to different order of the file list returned by jarFile.entries(). For specific logic, please see org.springframework.core.io.support.PathMatchingResourcePatternResolver#doFindPathMatchingJarResources (I am using springboot version 1.5.13). To solve this problem, it is necessary to do a natural sorting to solve the problem of inconsistent behavior of the same code in different environments.
in chinese: 我也遇到了同样的问题,为了找到最终的原因,我追踪了springboot的源码,很庆幸最终发现导致该问题的根本原因。原因在于,springboot bean的实例化顺序与从jar包返回的文件列表顺序有关。而打成jar包不同平台不一样导致了jarFile.entries()返回的文件列表顺序不同。具体逻辑请查看org.springframework.core.io.support.PathMatchingResourcePatternResolver#doFindPathMatchingJarResources (我使用的是springboot 1.5.13版本)。要想解决此问题,需要在做一次自然排序即可解决相同代码不同环境行为不一致的问题。
Agreed with @adamstyl, this is very annoying, when circular dependencies error occurs randomly only on CI/CD stand.
Hello all, working on the same problem in a project with Spring, K8s on Azure platform, where we tried two alternatives:
first we tried putting @Lazy annotation on the beans where we think caused the problem (not the best option), and
second [THE SOLUTION], reading the comments where a few folks mention the diferences building on Ubuntu desktop and Ubuntu server, we decided to try our docker image based on maven 3, jdk8 but with “maven-alpine” instead of “maven-ubuntu”. This was the solution for us. We hope this will be helpful.
Now this happened to me. I thought I was smart when I tried to decorate a bean for tests like this:
the intent, for the particular test package, is to take the actual implementation and stub out some methods with fakes. Is this a bad design? Yes, welcome to legacy codebases. Please do not suggest how to fix this in an OO-friendly way, I know that and will do the refactoring as soon as possible.
That said, this works on Windows. It does not on Linux, with either
BeanCurrentlyInCreationExceptionorNoSuchBeanDefinitionExceptiondepending on the exact strategy (I tried with no@Namedfirst, but also with@DependsOn, with adding a name to the original@Serviceetc., no luck so far).I understand my idea is probably wrong and only works by accident, but that’s the point - if it works locally I totally expect it to work on CI (or colleagues’ machines), too. Please make this deterministic. I do not really care which way - either fail all the time or make this work all the time. Even failing in some cases while working in others, when being consistent across environments, would be a better solution than this… 😦
Still happening, 2.1.4.RELEASE.