The dependencies of some of the beans in the application context form a cycle解决循环依赖

2年前 (2022) 程序员胖胖胖虎阿
127 0 0

今天遇到了一个循环依赖的问题:

The dependencies of some of the beans in the application context form a cycle解决循环依赖

之前一直听说过循环依赖,面试的时候也经常问循环依赖的问题,具体解决方案就是spring的三级缓存可以解决循环依赖问题

找了一下,说spring解决不了两种情况的循环依赖:

1. 非单例

2. 构造器注入

巧了,我的代码都不符合,所以按道理说,我的循环依赖问题spring完全可以自己解决的

想着看看源码吧,到底什么情况,但是我源码太不熟了,根本不知道入口在哪里,怎么看,只知道在populatBean的方法里面可以解决循环依赖的问题

重点来了,我又看了一遍报错信息,下面是 

The dependencies of some of the beans in the application context form a cycle解决循环依赖

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.

翻译过来就是:

 注意:

不鼓励依赖循环引用,默认情况下禁止使用循环引用。更新应用程序以消除bean之间的依赖循环。作为最后手段,有可能解决自动循环通过设置  spring.main.allow-circular-references to true

然后我就设置了一下

The dependencies of some of the beans in the application context form a cycle解决循环依赖

然后项目就可以正常启动了

总结:

1. springboot2.6开始,默认禁止循环依赖,而我的项目版本正好是2.6

2. 也可以通过代码的方式来解决循环依赖

相关文章

暂无评论

暂无评论...