【Spring】如何解决Spring的循环依赖问题?这道面试题助我拿到了Offer!!

最近,有关于Spring的面试中,Spring的循环依赖问题是必问的知识点,你回答的不好,就会被直接pass掉。即使你没被pass掉,你也会比那些知道如何解决Spring循环依赖问题的同事薪资低,这就是现实。所以,我们今天就一起来探讨下如何解决Spring循环依赖的问题。

关注 冰河技术 微信公众号,阅读更多技术干货文章!!

什么是循环依赖?


循环依赖其实就是循环引用,也就是两个或者两个以上的bean互相持有对方,最终形成闭环。比如A依赖于B,B依赖于C,C又依赖于A。如下图:

在这里插入图片描述

注意,这里不是函数的循环调用,是对象的相互依赖关系。循环调用其实就是一个死循环,除非有终结条件。

Spring中循环依赖场景有:

(1)构造器的循环依赖

(2)field属性的循环依赖

其中,构造器的循环依赖问题无法解决,只能拋出BeanCurrentlyInCreationException异常,在解决属性循环依赖时,spring采用的是提前暴露对象的方法。

怎么检测是否存在循环依赖?


检测循环依赖相对比较容易,Bean在创建的时候可以给该Bean打标,如果递归调用回来发现正在创建中的话,即说明了循环依赖了。

三种循环依赖


1.构造器的循环依赖。【这个Spring解决不了】

S

pring容器会将每一个正在创建的Bean 标识符放在一个“当前创建Bean池”中,Bean标识符在创建过程中将一直保持在这个池中,因此如果在创建Bean过程中发现自己已经在“当前创建Bean池”里时将抛出BeanCurrentlyInCreationException异常表示循环依赖;而对于创建完毕的Bean将从“当前创建Bean池”中清除掉。

Spring容器先创建单例A,A依赖B,然后将A放在“当前创建Bean池”中,此时创建B,B依赖C ,然后将B放在“当前创建Bean池”中,此时创建C,C又依赖A, 但是,此时A已经在池中,所以会报错,因为在池中的Bean都是未初始化完的,所以会依赖错误 ,(初始化完的Bean会从池中移除)

public class StudentA {

private StudentB studentB ;

public void setStudentB(StudentB studentB) {

this.studentB = studentB;

}

public StudentA() {

}

public StudentA(StudentB studentB) {

this.studentB = studentB;

}

}

public class StudentB {

private StudentC studentC ;

public void setStudentC(StudentC studentC) {

this.studentC = studentC;

}

public StudentB() {

}

public StudentB(StudentC studentC) {

this.studentC = studentC;

}

}

public class StudentC {

private StudentA studentA ;

public void setStudentA(StudentA studentA) {

this.studentA = studentA;

}

public StudentC() {

}

public StudentC(StudentA studentA) {

this.studentA = studentA;

}

}

上面是很基本的3个类,,StudentA有参构造是StudentB。StudentB的有参构造是StudentC,StudentC的有参构造是StudentA ,这样就产生了一个循环依赖的情况,

我们都把这三个Bean交给Spring管理,并用有参构造实例化

下面是测试类:

public class Test {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(“com/liuqing/student/applicationContext.xml”);

//System.out.println(context.getBean(“a”, StudentA.class));

}

}

执行结果报错信息为:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException:

Error creating bean with name ‘a’: Requested bean is currently in creation: Is there an unresolvable circular reference?

2.setter方式单例,默认方式

Spring中Bean实例化的图

![在这里插入图片描述](https://img-blog.csdnimg.cn/

20200817004920930.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2wxMDI4Mzg2ODA0,size_16,color_FFFFFF,t_70#pic_center)

如图中前两步骤得知:Spring是先将Bean对象实例化【依赖无参构造函数】—>再设置对象属性的

修改配置文件为set方式注入:

下面是测试类:

public class Test {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(“com/liuqing/student/applicationContext.xml”);

System.out.println(context.getBean(“a”, StudentA.class));

}

}

A" ref=“a”>

下面是测试类:

public class Test {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(“com/liuqing/student/applicationContext.xml”);

System.out.println(context.getBean(“a”, StudentA.class));

}

}

  • 18
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值