spring 在某些情況下是存在這樣的問題:
https://github.com/spring-projects/spring-framework/issues/18879
https://github.com/spring-projects/spring-framework/issues/24325
https://stackoverflow.com/questions/29347723/why-does-spring-get-circular-dependency-issues-on-one-machine-and-not-another
解決辦法:
1. 去循環依賴
2. @Lazy
3. InitializingBean 時,從 context 中獲取
4. @PostConstruct 去設置 bean 依賴
https://www.baeldung.com/circular-dependencies-in-spring
為什么在本地可以,上了測試環境就不行,或者上了生產環境就不行了?
答:根源在於在不同的操作系統或者環境下, bean 的加載順序是不固定的。bean 加載順序變化之后,就可能會導致循環依賴報錯問題產生!因為,順序變化之后,循環依賴的主體變了!
bean 加載時,會先將所有的 BeanDefinition 掃描出來,掃描出來的順序基本上就決定了 bean 的加載順序。
掃描 BeanDefiniton 的方法是 ClassPathScanningCandidateComponentProvider#scanCandidateComponents(),這個方法在不同的環境下掃描出類的順序是不固定的。
它的底層走的是 java.lang.ClassLoader#getResources() ,這個方法沒有承諾獲取到資源文件的順序!
1 // PathMatchingResourcePatternResolver#doFindAllClassPathResources 2 protected Set<Resource> doFindAllClassPathResources(String path) throws IOException { 3 Set<Resource> result = new LinkedHashSet<>(16); 4 ClassLoader cl = getClassLoader(); 5 Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path)); 6 while (resourceUrls.hasMoreElements()) { 7 URL url = resourceUrls.nextElement(); 8 result.add(convertClassLoaderURL(url)); 9 } 10 if (!StringUtils.hasLength(path)) { 11 // The above result is likely to be incomplete, i.e. only containing file system references. 12 // We need to have pointers to each of the jar files on the classpath as well... 13 addAllClassLoaderJarRoots(cl, result); 14 } 15 return result; 16 }
如果不清楚循環依賴的主體是什么意思,可以參考博主的另外一篇文章:https://blog.csdn.net/wang489687009/article/details/122284171#_61