先上代码
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
@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;
/**
* @author syz
*/
@Component
public class B {
@Resource
private A a;
}
执行以上代码出现以上错误,去分析了一下步骤
跟此文相关的bean创建的三个步骤,实例化 -> 初始化 -> 处理器生成代理
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;
}
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之前创建完毕则可避免此问题
因篇幅问题不能全部显示,请点此查看更多更全内容