您的浏览器过于古老 & 陈旧。为了更好的访问体验, 请 升级你的浏览器
Coder 发布于2021年11月26日 21:35 最近更新于 2021年11月26日 21:53

原创 解决升级 Spring Boot 2.6后,因循环引用导致启动时报错 BeanCurrentlyInCreationException 的问题

17033 次浏览 读完需要≈ 4 分钟 Spring BootTomcat

内容目录

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'xxxService': Requested bean is currently in creation: Is there an unresolvable circular reference?

升级 Spring Boot 2.6 后,在 Tomcat 中启动有可能出现以上异常。

有些人在通过 main() 方法启动应用时,也可能遇到以下异常:

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.main.allow-circular-references=true

这是因为从 2.6 开始,Spring Boot 默认禁用了类似如下的循环引用:

	@Component
	public class A {

		@Autowired
		protected B b;

	}

	@Component
	public class B {

		@Autowired
		protected A a;

	}

Spring Boot 2.6 还将 Spring MVC 默认的请求路径匹配策略从 AntPathMatcher 变更为了PathPatternParser

你可以访问官方文档查看更多变更详情:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6-Release-Notes

  • CodePlayer技术交流群1
  • CodePlayer技术交流群2

0 条评论

撰写评论