spring如何解决循环依赖

Spring容器循环依赖包括构造器循环依赖和setter循环依赖,那Spring容器如何解决循环依赖呢?首先让咱们来定义循环引用类:php

 

  1.  public class TestA {  
  2.  
  3.     private TestB testB;  
  4.  
  5.     public void a() {  
  6.         testB.b();  
  7.     }  
  8.  
  9.     public TestB getTestB() {  
  10.         return testB;  
  11.     }  
  12.  
  13.     public void setTestB(TestB testB) {  
  14.         this.testB = testB;  
  15.     }  
  16. }  
  17.  
  18. public class TestB {  
  19.     private TestC testC;  
  20.  
  21.     public void b() {  
  22.         testC.c();  
  23.     }  
  24.  
  25.     public TestC getTestC() {  
  26.         return testC;  
  27.     }  
  28.  
  29.     public void setTestC(TestC testC) {  
  30.         this.testC = testC;  
  31.     }  
  32. }  
  33.  
  34.  
  35. public class TestC {  
  36.     private TestA testA;  
  37.  
  38.     public void c() {  
  39.         testA.a();  
  40.     }  
  41.  
  42.     public TestA getTestA() {  
  43.         return testA;  
  44.     }  
  45.  
  46.     public void setTestA(TestA testA) {  
  47.         this.testA = testA;  
  48.     }  
  49. }  

在Spring中将循环依赖的处理分红了3种状况。html

1.构造器循环依赖缓存

表示经过构造器注入构成的循环依赖,此依赖是没法解决的,只能抛出BeanCurrentlyIn CreationException异常表示循环依赖。学习

如在建立TestA类时,构造器须要TestB类,那将去建立TestB,在建立TestB类时又发现须要TestC类,则又去建立TestC,最终在建立TestC时发现又须要TestA,从而造成一个环,没办法建立。测试

Spring容器将每个正在建立的bean标识符放在一个"当前建立bean池"中,bean标识符在建立过程当中将一直保持在这个池中,所以若是在建立bean过程当中发现本身已经在"当前建立bean池"里时,将抛出BeanCurrentlyInCreationException异常表示循环依赖;而对于建立完毕的bean将从"当前建立bean池"中清除掉。this

咱们经过一个直观的测试用例来进行分析。spa

(1)建立配置文件。prototype

 

  1. <bean id="testA" class="com.bean.TestA">     
  2.     <constructor-arg index="0" ref="testB"/>     
  3. </bean>     
  4. <bean id="testB" class="com.bean.TestB">     
  5.     <constructor-arg index="0" ref="testC"/>     
  6. </bean>     
  7. <bean id="testC" class="com.bean.TestC">     
  8.     <constructor-arg index="0" ref="testA"/>     
  9. </bean>     

(2)建立测试用例。code

 

  1. @Test(expected = BeanCurrentlyInCreationException.class)     
  2. public void testCircleByConstructor() throws Throwable {     
  3.     try {     
  4.              new ClassPathXmlApplicationContext("test.xml");     
  5.     } catch (Exception e) {     
  6.       //由于要在建立testC时抛出;     
  7.       Throwable ee1 = e.getCause().getCause().getCause();     
  8.       throw e1;     
  9.     }     
  10. }   

 

针对以上代码的分析以下。xml

Spring容器建立"testA"bean,首先去"当前建立bean池"查找是否当前bean正在建立,若是没发现,则继续准备其须要的构造器参数"testB",并将"testA"标识符放到"当前建立bean池"。

Spring容器建立"testB"bean,首先去"当前建立bean池"查找是否当前bean正在建立,若是没发现,则继续准备其须要的构造器参数"testC",并将"testB"标识符放到"当前建立bean池"。

Spring容器建立"testC"bean,首先去"当前建立bean池"查找是否当前bean正在建立,若是没发现,则继续准备其须要的构造器参数"testA",并将"testC"标识符放到"当前建立Bean池"。

到此为止Spring容器要去建立"testA"bean,发现该bean标识符在"当前建立bean池"中,由于表示循环依赖,抛出BeanCurrentlyInCreationException。

2.setter循环依赖

表示经过setter注入方式构成的循环依赖。对于setter注入形成的依赖是经过Spring容器提早暴露刚完成构造器注入但未完成其余步骤(如setter注入)的bean来完成的,并且只能解决单例做用域的bean循环依赖。经过提早暴露一个单例工厂方法,从而使其余bean能引用到该bean,以下代码所示:

 

  1. addSingletonFactory(beanName, new ObjectFactory() {     
  2.     public Object getObject() throws BeansException {     
  3.         return getEarlyBeanReference(beanName, mbd, bean);     
  4.     }     
  5. });    

具体步骤以下。

(1)Spring容器建立单例"testA"bean,首先根据无参构造器建立bean,并暴露一个"ObjectFactory"用于返回一个提早暴露一个建立中的bean,并将"testA"标识符放到"当前建立bean池",而后进行setter注入"testB"。

(2)Spring容器建立单例"testB"bean,首先根据无参构造器建立bean,并暴露一个"ObjectFactory"用于返回一个提早暴露一个建立中的bean,并将"testB"标识符放到"当前建立bean池",而后进行setter注入"circle"。

(3)Spring容器建立单例"testC"bean,首先根据无参构造器建立bean,并暴露一个"ObjectFactory"用于返回一个提早暴露一个建立中的bean,并将"testC"标识符放到"当前建立bean池",而后进行setter注入"testA"。进行注入"testA"时因为提早暴露了"ObjectFactory"工厂,从而使用它返回提早暴露一个建立中的bean。

(4)最后在依赖注入"testB"和"testA",完成setter注入。

3.prototype范围的依赖处理

对于"prototype"做用域bean,Spring容器没法完成依赖注入,由于Spring容器不进行缓存"prototype"做用域的bean,所以没法提早暴露一个建立中的bean。示例以下:

(1)建立配置文件。

 

  1. <bean id="testA" class="com.bean.CircleA" scope="prototype">     
  2.       <property name="testB" ref="testB"/>     
  3.  </bean>     
  4.  <bean id="testB" class="com.bean.CircleB" scope="prototype">     
  5.      <property name="testC" ref="testC"/>     
  6.  </bean>     
  7.  <bean id="testC" class="com.bean.CircleC" scope="prototype">     
  8.     <property name="testA" ref="testA"/>     
  9.  </bean>    

(2)建立测试用例。

 

  1. @Test(expected = BeanCurrentlyInCreationException.class)     
  2. public void testCircleBySetterAndPrototype () throws Throwable {     
  3.     try {     
  4.         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(     
  5. "testPrototype.xml");     
  6.         System.out.println(ctx.getBean("testA"));     
  7.     } catch (Exception e) {     
  8.         Throwable ee1 = e.getCause().getCause().getCause();     
  9.         throw e1;     
  10.     }     
  11. }    

对于"singleton"做用域bean,能够经过"setAllowCircularReferences(false);"来禁用循环引用。

感谢互联网时代,让我能够方便地获取我想要的各类信息,当初我刚开始学习的时候,一直纠结于这里错综复杂的逻辑,幸亏我看到了一篇文章解开了我心中的疑惑。在此,感谢原做者,并将原文与你们分享,帮助你们更好的理解Spring的依赖,你们能够从http://www.iflym. com/index.php/code/201208280001.html来获取原文。