SpringBoot项目处理循环依赖问题(circular reference)

本文记录了一次SpringBoot启动失败的问题,错误为BeanCurrentlyInCreationException,原因是循环依赖。解决方案是在相关Service上使用@Lazy注解打破循环。作者反思自己没有一开始就寻求解决方案,导致花费更多时间。

1 问题

  • springBoot启动失败,报错BeanCurrentlyInCreationException
org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'AxxxxService': 
Bean with name 'BxxxxService' 
has been injected into other beans [CxxxxService] in its raw version as part of a circular reference, 
but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - 
consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example. 

2 解决方法

  • 1 找到中括号中的Service: CxxxxService
  • 2 在CxxxxService中找到 BxxxxService(离得最近的,如果看不懂英文的话)的声明
  • 3 在BxxxService的声明上加注解 @Lazy
    注: 如果中括号中不止一个CxxxxService,每个都要这样做,加上@Lazy

3 悔恨

  • 我没有一开始就百度,解决方案,而是自以为是地组装新的Service类
  • 今天遇到一个中括号里有多个类的,要改的多了,就搜索一下人家的解决方案,流下了悔恨的眼泪
  • 现在才把代码调整回去了,已经晚上10点多了
  • 本来想试试revert, 过去太久,太多commit了,害怕了,退缩了
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值