由@Async引起的Spring循环引用
发布日期:2022-02-17 07:12:02 浏览次数:1 分类:技术文章

本文共 2093 字,大约阅读时间需要 6 分钟。

场景&原因

先上代码

package com.shi.cycleref;import org.springframework.scheduling.annotation.Async;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.stereotype.Component;import javax.annotation.Resource;/** * @author syz */@EnableAsync@Componentpublic class A {
@Resource private B b; @Async public void a () {
}}
package com.shi.cycleref;import org.springframework.context.annotation.Lazy;import org.springframework.stereotype.Component;import javax.annotation.Resource;/** * @author syz */@Componentpublic class B {
@Resource private A a;}

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘a’: Bean with name ‘a’ has been injected into other beans [b] 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 ‘getBeanNamesForType’ with the ‘allowEagerInit’ flag turned off, for example.

执行以上代码出现以上错误,去分析了一下步骤

跟此文相关的bean创建的三个步骤,实例化 -> 初始化 -> 处理器生成代理

  1. 先实例化A,初始化A时发现依赖B,缓存A
  2. 实例化B,初始化B时发现依赖A,缓存中取到A注入,B完成
  3. 返回初始化A步骤把刚刚生成好的B注入,发现A用到@Async注解生成A的代理类,造成了B中注入的A是未被代理的类(不是最终版本)

解决方法

  1. 在B类中A属性上加@Lazy注解,在使用A时才会注入A
package com.shi.cycleref;import org.springframework.context.annotation.Lazy;import org.springframework.stereotype.Component;import javax.annotation.Resource;/** * @author syz */@Componentpublic class B {
@Resource @Lazy private A a;}
  1. 在A类上使用@DependsOn注解,创建A时会判断是否有此注解,有则先去创建标明的类
package com.shi.cycleref;import org.springframework.context.annotation.DependsOn;import org.springframework.scheduling.annotation.Async;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.stereotype.Component;import javax.annotation.Resource;/** * @author syz */@EnableAsync@Component@DependsOn("b")public class A {
{
System.out.println("AAAAAAAAAAAAA"); } @Resource private B b; @Async public void a () {
}}

注意

主要是一个初始化顺序的问题,A在B之前创建完毕则可避免此问题

转载地址:https://blog.csdn.net/qq_42559485/article/details/115749978 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:面向对象
下一篇:SpringBoot自动配置

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月10日 21时21分16秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章