Spring 在启动时检测到循环依赖(circular dependency)

1、异常信息

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException:  
Error creating bean with name 'asyncUserDetailService':  
Bean with name 'asyncUserDetailService' has been injected into other beans [asyncCompanyService] 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.

这段异常信息是 Spring 在启动时检测到 循环依赖(circular dependency) 后给出的详细说明。我们来逐句拆解它的含义,并给出具体的解决办法。

2、异常信息逐句解释

(1)BeanCurrentlyInCreationException: Error creating bean with name 'asyncUserDetailService'
Spring 正在创建 asyncUserDetailService 这个 Bean,但在这个过程中出错了。

(2)Bean with name 'asyncUserDetailService' has been injected into other beans [asyncCompanyService] in its raw version
asyncUserDetailService被注入到了 asyncCompanyService中,但注入的是它的“原始版本”(未完成初始化),因为此时它还在创建中。

(3)but has eventually been wrapped. This means that said other beans do not use the final version of the bean
Spring 最终为这个 Bean 生成了代理(wrapped),但注入到其他 Bean 中的却是早期未代理的版本,这可能导致运行时行为不一致。

(4)This is often the result of over-eager type matching
这类问题通常是因为 Spring 在早期就尝试解析依赖,导致注入了未完成初始化的 Bean。

3、解决办法

使用 @Lazy 延迟注入(推荐)

在 AsyncCompanyService 中,对 AsyncUserDetailService 使用 @Lazy 注解,让 Spring 延迟加载它:

@Autowired
@Lazy
private AsyncUserDetailService asyncUserDetailService;
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YD_1989

分享不易,非常感谢您的鼓励支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值