首页 > 其他分享 >升级spring boot异常:spring循环依赖

升级spring boot异常:spring循环依赖

时间:2023-06-02 18:14:40浏览次数:26  
标签:依赖 spring boot 循环 references circular main cycle

问题
从spring boot 2.2.9升级到2.6.2版本后,项目启动后访问报错
The dependencies of some of the beans in the application context form a cycle.

serviceCollectionIdCacheService
┌─────┐
| serviceProductInfoProviderImpl
↑ ↓
| serviceOfflineProviderImpl
↑ ↓
| serviceProductMappingProviderImpl
└─────┘

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.

原因
在2.6.0之前,spring会自动处理循环依赖的问题,2.6.0 以后的版本默认禁止 Bean 之间的循环引用,如果存在循环引用就会启动失败报错。

解决
方案1:
清理循环引用的Bean
1、在字段上使用@Autowired注解,让Spring决定在合适的时机注入。
2、在@Autowired注解上方加上@Lazy注解(延迟加载)
(A—>B—>C—>D 一般在D引用A的@Autowired下加入@Lazy注解即可)

方案2:
it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
也可以暂时跳过,在yml配置中加入

spring:
main:
allow-circular-references: true

方案3:
在启动中加入也一样

public static void main(String[] args) {
SpringApplication sa = new SpringApplication(xx.class);
sa.setAllowCircularReferences(Boolean.TRUE);//加入的参数
sa.run(args);
}
————————————————
版权声明:本文为CSDN博主「SangBigYe」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/CutelittleBo/article/details/122411294

标签:依赖,spring,boot,循环,references,circular,main,cycle
From: https://www.cnblogs.com/telwanggs/p/17452606.html

相关文章

  • 基于 Vue BootStrap的迷你Chrome插件
    安装安装VisualStudioCode和Chrome,自行翻墙详细安装这里略过安装包管理工具用管理员身份运行cmd,输入:npminstall-gbower(全局安装)创建新建目录brochure进入目录运行bowerinstallbootstrap@3vueaxios成功!新建css,js目录,并在对应的目录下面新建index.css和inde......
  • springboot - 项目启动初始化数据
    1、redis配置依赖<!--redis--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>redisconfi......
  • SpringBoot大文件分片上传/多线程上传
    ​ 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数下面直接贴代码吧,一些难懂的我大部分都加上注释了:上传文件实体类:看得出来,实体类中已经有很多我们需要的功能了,还有实用的属性。如MD5秒传的信息。pub......
  • Spring配置数据源
    1.Spring配置数据源1.1数据源(连接池)的作用数据源(连接池)是提高程序性能如出现的事先实例化数据源,初始化部分连接资源使用连接资源时从数据源中获取使用完毕后将连接资源归还给数据源常见的数据源(连接池):DBCP、C3P0、BoneCP、Druid等开发步骤①导入数据源的坐标和数据......
  • Spring核心接口之InitializingBean
    一、InitializingBean接口说明InitializingBean接口为bean提供了属性初始化后的处理方法,它只包括afterPropertiesSet方法,凡是继承该接口的类,在bean的属性初始化后都会执行该方法。packageorg.springframework.beans.factory;/***Interfacetob......
  • Spring核心接口之Ordered
    一、Ordered接口介绍Spring中提供了一个Ordered接口。从单词意思就知道Ordered接口的作用就是用来排序的。Spring框架是一个大量使用策略设计模式的框架,这意味着有很多相同接口的实现类,那么必定会有优先级的问题。于是Spring就提供了Ordered这个接口,来处......
  • 如何使用Spring管理Filter和Servlet
    在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象的创建。如果要在filter或者servlet中使用spring容器管理业务对象,通常需要使用WebApplicationContextUtils.getRequiredWebApplicationContext......
  • bootstrap.bundle.min.js bootstrap.bundle.min.js.map 404报错
    main.js导入 import'./assets/bootstrap.bundle.min.js'浏览器报如下告警信息:DevToolsfailedtoloadsourcemap:Couldnotloadcontentforhttp://localhost:5173/src/assets/bootstrap.bundle.min.js.map:HTTP错误:状态代码404,net::ERR_HTTP_RESPONSE_CODE_FAILU......
  • spring为什么注入接口而不是实现类?
    首先,一般使用接口是很常用并且有益的变成技术。其次,在spring中,你可以在运行过程中注入各种实现。一个很经典的情况就是在测试阶段,注入模拟的实现类。===1.网上说jdk动态代理基于实现接口。直接注入实现类会使aop失效。没有cglib可能真的就失效了。2.解耦。假如有一天实现类的名......
  • springboot项目rabbitmq消费者消费json格式的String,出现无限循环抛出No method found
    转:springboot项目rabbitmq消费者消费json格式的String,出现无限循环抛出Nomethodfoundforclass[B     ......