采用 Spring Boot 2.6 因循环引用导致启动时报错的问题

1,347 阅读1分钟

今天在使用 Spring Boot 2.6 遇到一个问题,卷了好久才卷出来。原来是 Spring Boot 2.6 开始禁止循环依赖,做了依赖循环的检查。

作为程序猿的我们,遇到循环依赖正是头大,但是这个问题我们无法避免,只能通过手段解决,要么修改代码。

在 Spring Boot 项目启动时,抛出如下异常详情:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
13.060 ERROR o.s.b.d.LoggingFailureAnalysisReporter:40   restartedMain                    

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

┌──->──┐
|  com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration
└──<-──┘


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.


Process finished with exit code 0

解决办法使用解除循环依赖,那么该如何解决呢,需要我们在配置文件 application.properties 或者 application.yml 里加上这个属性: properties 配置文件:

spring.main.allow-circular-references = true

yml 配置文件:

spring.main:
  allow-circular-references: true

这个属性默认是 false,需要设置成 true。这只是其中的一种解决方案。