Springboot 使用升级小记-循环依赖

本文讲述了如何在SpringBoot从2.2.6升级到2.7.16或更高版本时处理循环依赖问题,推荐在application.properties或application.yaml中启用`spring.main.allow-circular-references`,以及在SpringCloud项目中的配置方法,强调了查阅官方文档的重要性。
摘要由CSDN通过智能技术生成

springboot 使用已经非常广泛了,它的版本迭代速度也比较快,过一段时间版本差异就会比较大。

由于低版本依赖的 spring 版本有漏洞问题,这次我们是从 2.2.6 版本直接升级到 2.7.16,升级 3.0 的话担心差异更大。

这时直接修改依赖启动就会报错,主要是提示循环依赖的问题,这里就不贴代码了,大致是这样的错误

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 一直不建议循环依赖,但是没办法还得兼容,这次 springboot2.6 这个版本直接给禁用了,你要使用要么去掉循环依赖,要么使用兼容配置。想必大家要在老项目去掉循环依赖都觉得头大吧。那就这样来搞:

通过在 application.properties 或 application.yaml 配置打开允许循环依赖

application.properties

spring.main.allow-circular-references=true

application.yaml

spring:
  main:
    allow-circular-references: true

如果你使用了 springcloud 时,在没有以上的配置文件时,也可以在
resource/bootstrap.yaml 中增加配置:

spring:
  main:
    allow-circular-references: true

下面是参考资料

禁用循环依赖:https://www.springcloud.io/post/2022-02/spring-cyclic-dependencies/#gsc.tab=0

其他解决方案:https://medium.com/@karthik.jeyapal/circular-dependency-in-spring-boot-how-to-detect-and-fix-it-2a6e64bb488f

核心就是要对升级的内容做到心中有数,盲目升级就会出现各种搜索解决方案,所以建议大家多了解官方文档,比如迁移建议、更新内容等

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值