【报错解决】The dependencies of some of the beans in the application context form a cycle:

问题描述

项目启动中报错,错误信息如下:

2022-03-31 17:26:51.877 ERROR 24308 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

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

┌─────┐
|  mvcConfigurer (field private org.springframework.boot.autoconfigure.http.HttpMessageConverters com.tmsj.supert.system.config.MvcConfigurer.httpMessageConverters)
↑     ↓
|  messageConverters defined in class path resource [org/springframework/boot/autoconfigure/http/HttpMessageConvertersAutoConfiguration.class]
└─────┘


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.

原因分析:

循环依赖报错

在用springboot启动项目时,报了这个互相依赖的错误,原因就是 Aservice 需要调用 Bservice ,然后Bservice 又需要调用 Aservice ,所以直接依赖循环了。

循环依赖指的是两个或者多个bean之间相互依赖,形成一个闭环。直接表现为两个service层互相调用对方。


解决方案:

第一种:

在 主 Application中 main方法新增,即可解决

		SpringApplication springApplication = new SpringApplication(SystemApplication.class);
		springApplication.setAllowCircularReferences(Boolean.TRUE);
		springApplication.run(args);

第二种

使用@Lazy注解
解决Spring 循环依赖的一个简单方法就是对一个Bean使用延时加载。也就是说:这个Bean并没有完全的初始化完,实际上他注入的是一个代理,只有当他首次被使用的时候才会被完全的初始化。

    @Lazy
    @Autowired
    private BootSerivice bootSerivice;

第三种:

通过修改配置文件来解决

spring:
	main:
		allow-circular-references: true
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值