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

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了,害怕了,退缩了
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环依赖是指两个或多个类相互引用对方,导致在初始化bean时出现无法确定先后顺序的情况。在Spring Boot中,循环依赖是不鼓励的,默认情况下是禁止的。当出现循环依赖时,Spring Boot会抛出一个错误,并提供解决方法。 解决循环依赖的方式有两种: 1. 延迟加载:可以通过使用@Lazy注解来延迟加载其中一个bean,从而解决初始化顺序的问题。在需要互相依赖的bean上添加@Lazy注解,使其在真正被使用时才进行初始化。例如,可以在@Autowired注解上添加@Lazy注解: ``` @Autowired @Lazy private TokenService tokenService; ``` 2. 修改配置来打破循环:可以通过修改配置文件来允许循环依赖。在application.yml或application.properties文件中添加配置项spring.main.allow-circular-references: true即可允许循环依赖。请注意,这只是作为最后的手段,不推荐经常使用。 总之,循环依赖是一种不鼓励且默认禁止的情况,在Spring Boot中可以通过延迟加载或修改配置来解决循环依赖问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [springboot解决循环依赖问题](https://blog.csdn.net/qq_41175917/article/details/125912639)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值