Spring 切换到新版本后突然有循环依赖

Spring suddenly has cyclic dependencies after switching to new version

一个 Maven 项目有 spring-boot-starter-parent 作为它的父项目。我刚刚将版本从 2.3.1.RELEASE 切换到 2.6.7.

突然,使用此项目的 spring 启动应用程序具有循环依赖性并出现此错误:

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

   org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
┌─────┐
|  com.mycompany.spring.logging.configuration.InterceptorConfiguration (field com.mycompany.spring.logging.RequestInterceptor com.mycompany.spring.logging.configuration.InterceptorConfiguration.interceptor)
└─────┘


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.

没有 Java 源已更改,其他依赖项没有更改。错误中的类(InterceptorConfiguration和RequestInterceptor)在另一个内部库中,并没有改变。

我对导致循环引用的原因有点困惑,我不确定如何解决它。设置 spring.main.allow-circular-references=true 似乎不是一个理想的解决方案。

根据文档, https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6.0-M2-Release-Notes#circular-references-prohibited-by-default,默认不禁止循环引用。通过将 spring.main.allow-circular-references 设置为 true 可以再次允许循环引用
因此,您的应用程序有可能早些时候也有循环依赖,但在默认情况下 Spring 允许它工作。最佳解决方案是识别并删除循环引用。