使用springboot测试循环依赖问题
问题出现
编写了两个类来测试一下循环依赖:
TestA类
@ComponentpublicclassTestA{@AutowiredprivateTestB testB;publicvoidaFunc(){System.out.println("TestA");}}
TestB类
@ComponentpublicclassTestB{@AutowiredprivateTestA a;publicvoidbFunc(){System.out.println("TestB");}}
没想到启动报错了
Description:The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| testA (field privatecom.example.springtest.TestBcom.example.springtest.TestA.testB)
↑ ↓
| testB (field privatecom.example.springtest.TestAcom.example.springtest.TestB.a)
└─────┘
Action:Relying upon circular references is discouraged and they are prohibited by default. Update your application toremove the dependency cycle between beans. As a last resort, it may be possible tobreak the cycle automatically by setting spring.main.allow-circular-references totrue
可是印象里,spring不是默认支持循环依赖的吗?哎?
问题溯源
首先按照之前对spring循环依赖的理解,我们知道spring有三级缓存来解决循环依赖的问题,假设先创建的是TestA,那么存在以下的推导:
doCreateBean(TestA) -> populateBean(TestA) -> doCreateBean(TestB) -> populateBean(TestB) -> TestB添加到IOC容器中 -> TestA添加的IOC容器中
那为啥还是出现了循环依赖问题呢?根据报错提示我们可以设置
spring.main.allow-circular-references
为true来解决循环依赖问题,这个配置在spring中不说默认为true吗,难道是springboot对这个配置进行了修改?
在doCreateBean中有一段关键的代码
// Eagerly cache singletons to be able to resolve circular references// even when triggered by lifecycle interfaces like BeanFactoryAware.boolean earlySingletonExposure =(mbd.isSingleton()&&this.allowCircularReferences &&isSingletonCurrentlyInCreation(beanName));if(earlySingletonExposure){if(logger.isTraceEnabled()){
logger.trace("Eagerly caching bean '"+ beanName +"' to allow for resolving potential circular references");}addSingletonFactory(beanName,()->getEarlyBeanReference(beanName, mbd, bean));}
通过断点调试可以看到this.allowCircularReferences的值为false
earlySingletonExposure为false,那三级缓存中就没有TestA,在populateBean(TestB)中又会走getSingleton(),最终在beforeSingletonCreation抛出了循环依赖的异常。
/** Cache of singleton factories: bean name to ObjectFactory. */privatefinalMap<String,ObjectFactory<?>> singletonFactories =newHashMap<>(16);
至于this.allowCircularReferences的值为什么默认是false,我的springboot的版本使用的是2.7.x,然后
2.6版本默认不支持循环依赖了
解决方法
在application.properties中,设置
spring.main.allow-circular-references=true
版权归原作者 小码鸽 所有, 如有侵权,请联系我们删除。