了解专栏完整的内容,请点击跳转:
Spring Framework专栏导航页
上一篇文章我们了解了Spring Bean的生命周期,当时提到了Bean的循环依赖问题,本文就再次展开描述该问题,看Spring是如何解决循环依赖的。
什么是循环依赖
@Component
public class A {
@Autowired
private B b;
}
@Component
public class B {
@Autowired
private A a;
}
如上述代码所示,就是循环依赖。其实在我们手动去new
对象的时候,这根本不是什么问题,为什么呢?
怎么解决循环依赖
public class Test {
public static void main(String[] args) {
A a = new A();
B b = new B();
a