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;

3888

被折叠的 条评论
为什么被折叠?



