https://www.cnblogs.com/liuqing576598117/p/11227007.html


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管理,並用有參構造實例化
<bean id="a" class="com.liuqing.student.StudentA"> <constructor-arg index="0" ref="b"></constructor-arg> </bean> <bean id="b" class="com.liuqing.student.StudentB"> <constructor-arg index="0" ref="c"></constructor-arg> </bean> <bean id="c" class="com.liuqing.student.StudentC"> <constructor-arg index="0" ref="a"></constructor-arg> </bean>
下面是測試類:
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實例化的圖
如圖中前兩步驟得知:Spring是先將Bean對象實例化【依賴無參構造函數】--->再設置對象屬性的
<!--scope="singleton"(默認就是單例方式) --> <bean id="a" class="com.liuqing.student.StudentA" scope="singleton"> <property name="studentB" ref="b"></property> </bean> <bean id="b" class="com.liuqing.student.StudentB" scope="singleton"> <property name="studentC" ref="c"></property> </bean> <bean id="c" class="com.liuqing.student.StudentC" scope="singleton"> <property name="studentA" ref="a"></property> </bean>
下面是測試類:
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)); } }
打印結果為:
com.liuqing.student.StudentA@1fbfd6
我們結合上面那張圖看,Spring先是用構造實例化Bean對象 ,此時Spring會將這個實例化結束的對象放到一個Map中,並且Spring提供了獲取這個未設置屬性的實例化對象引用的方法。 結合我們的實例來看,,當Spring實例化了StudentA、StudentB、StudentC后,緊接着會去設置對象的屬性,此時StudentA依賴StudentB,就會去Map中取出存在里面的單例StudentB對象,以此類推,不會出來循環的問題
3、setter方式原型,prototype
修改配置文件為:
<bean id="a" class="com.liuqing.student.StudentA" scope="prototype"> <property name="studentB" ref="b"></property> </bean> <bean id="b" class="com.liuqing.student.StudentB" scope="prototype"> <property name="studentC" ref="c"></property> </bean> <bean id="c" class="com.liuqing.student.StudentC" scope="prototype"> <property name="studentA" ref="a"></property> </bean>
scope="prototype" 意思是 每次請求都會創建一個實例對象。兩者的區別是:有狀態的bean都使用Prototype作用域,無狀態的一般都使用singleton單例作用域。
測試用例:
public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("com/liuqing/student/applicationContext.xml"); //此時必須要獲取Spring管理的實例,因為現在scope="prototype" 只有請求獲取的時候才會實例化對象 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?
對於“prototype”作用域Bean,Spring容器無法完成依賴注入,因為“prototype”作用域的Bean,Spring容器不進行緩存,因此無法提前暴露一個創建中的Bean。
四、Spring怎么解決循環依賴


三級singletonFactories : 單例對象工廠的cache