场景&原因

先上代码

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;

/**
 * 
 */
@EnableAsync
@Component
public 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;

/**
 *
 */
@Component
public 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创建的三个步骤,实例化 -> 初始化 -> 处理器生成代理

  • 先实例化A,初始化A时发现依赖B,缓存A

  • 实例化B,初始化B时发现依赖A,缓存中取到A注入,B完成

  • 返回初始化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
 */
@Component
public 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之前创建完毕则可避免此问题

文章作者: 刘同学
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 刘同学的小站
后端 Java
喜欢就支持一下吧