使用springboot测试循环依赖问题
问题出现
编写了两个类来测试一下循环依赖:
TestA类
@Component
public class TestA {
@Autowired
private TestB testB;
public void aFunc() {
System.out.println("TestA");
}
}
TestB类
@Component
public class TestB {
@Autowired
private TestA a;
public void bFunc() {
System.out.println("TestB");
}
}
没想到启动报错了
Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| testA (field private com.example.springtest.TestB com.example.springtest.TestA.testB)
↑ ↓
| testB (field private com.example.springtest.TestA com.example.springtest.TestB.a)
└─────┘
Action:
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true
可是印象里,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中有一段关键的代码
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抛出了循环依赖的异常。
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);
至于this.allowCircularReferences的值为什么默认是false,我的springboot的版本使用的是2.7.x,然后 2.6版本默认不支持循环依赖了
解决方法
在application.properties中,设置spring.main.allow-circular-references=true
|