IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> 使用springboot测试循环依赖问题 -> 正文阅读

[Java知识库]使用springboot测试循环依赖问题

使用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中有一段关键的代码

// 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
在这里插入图片描述

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-09-25 23:07:17  更:2022-09-25 23:08:43 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年3日历 -2024/3/29 20:06:37-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码