Spring Boot应用程序升级到2.6.0后由于循环依赖而无法启动[无法解析的循环引用]

Thi*_*mal 29 java spring-boot java-17

Spring boot升级报错

Spring Boot应用程序升级到2.6.0后由于循环依赖而无法启动

Error creating bean with name 'securityConfig': 
Requested bean is currently in creation: Is there an 
      unresolvable circular reference?
Run Code Online (Sandbox Code Playgroud)

Thi*_*mal 66

Circular References Prohibited by Default在 Spring Boot 2.6 版本中

\n

如果您的应用程序由于 BeanCurrentlyInCreationException 而无法启动,强烈建议您更新配置以打破依赖循环。

\n

临时解决方案是恢复 2.5\xe2\x80\x99s 行为,在 中设置以下内容.properties/yml

\n
spring: \n  main:\n    allow-circular-references: true\n
Run Code Online (Sandbox Code Playgroud)\n

请参阅这篇文章以获取深入的描述和一些可能的补救措施。

\n

  • 如果使用字段注入,循环依赖通常不是问题。仅当您 a) 使用构造函数注入或 b) 在访问“@Autowired”字段的循环的多个成员中具有容器触发的方法(例如“@PostConstruct”)时,它们才会成为问题。除此之外,bean 之间的循环依赖是无害的。 (13认同)
  • 我尝试在 application.properties 文件中使用它,如“spring.main.allow-circular-references=true”,但不起作用 (5认同)