开启循环依赖参数allowCircularReferences

使用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容器中
1

那为啥还是出现了循环依赖问题呢?根据报错提示我们可以设置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. */
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
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值