什麼是循環依賴?
循環依賴就是循環引用,指兩個或多個bean互相持有對方,比如說TestA引用TestB、TestB引用TestA,最終形成一個閉環。
注意:循環依賴不是指循環調用。
循環調用:指方法之間的環調用,循環調用是無解的,除非有終結條件,否則就是死循環,最終會導致內存溢出異常。
兩種Spring容器循環依賴:
- 構造器循環依賴
- setter方法循環依賴
Spring是如何解決的?
我們先定義循環依賴的實體代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
//bean1
public class TestA {
private TestB testB;
TestA(TestB testB){
this .testB=testB;
}
public TestB getTestB() {
return testB;
}
public void setTestB(TestB testB) {
this .testB = testB;
}
}
//bean2
public class TestB {
private TestA testA;
TestB(TestA testA){
this .testA=testA;
}
public TestA getTestA() {
return testA;
}
public void setTestA(TestA testA) {
this .testA = testA;
}
}
|
在Spring中循環依賴處理分爲3種情況
1. 構造器循環依賴(無法解決)
表示通過構造器注入構成的循環依賴,此依賴是無解的,強行依賴只能拋出異常(BeanCreationException);
Spring容器將每一個正在創建的bean標識符放在一個“當前創建bean池”中,bean標識符在創建過程中將一直保持在這個池中,因此在創建bean的過程中如果發現自己已經在池中,則拋出BeanCurrentlyInCreationException異常表示循環依賴;而對於創建完畢的bean將從“當前創建bean池”中清除掉。
下面我們通過一段代碼來印證上述理論。
創建application.xml配置文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version= "1.0" encoding= "UTF-8" ?>
<beans xmlns= "http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http: //www.springframework.org/schema/beans
http: //www.springframework.org/schema/beans/spring-beans.xsd">
<bean id= "testA" class = "com.chenpt.springFrameWork.TestA" >
<constructor-arg name= "testB" ref= "testB" ></constructor-arg>
</bean>
<bean id= "testB" class = "com.chenpt.springFrameWork.TestB" >
<constructor-arg name= "testA" ref= "testA" ></constructor-arg>
</bean>
</beans>
|
客戶端測試用例:
1
2
3
4
5
6
|
public class MainTest {
public static void main(String[] args){
ApplicationContext context = new FileSystemXmlApplicationContext( "classpath:spring/applicationContext.xml" );
}
}
|
執行結果如下圖(僅截了部分錯誤)
2. setter循環依賴(可以解決)
指通過setter注入方式構成的循環依賴。
解決方式:Spring容器提前暴露剛完成構造器注入但未完成其他步驟(如setter注入)的bean來完成的。而且只能解決單例作用域的bean循環依賴。通過提前暴露一個單例工廠方法,從而使其他bean能引用到該bean。
代碼示例:
首先需要去掉構造器注入的參數。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
//bean1
public class TestA {
private TestB testB;
TestA(){}
public TestB getTestB() {
return testB;
}
public void setTestB(TestB testB) {
this .testB = testB;
}
}
//bean2
public class TestB {
private TestA testA;
TestB(){}
public TestA getTestA() {
return testA;
}
public void setTestA(TestA testA) {
this .testA = testA;
}
}
|
xml示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version= "1.0" encoding= "UTF-8" ?>
<beans xmlns= "http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http: //www.springframework.org/schema/beans
http: //www.springframework.org/schema/beans/spring-beans.xsd">
<bean id= "testA" class = "com.chenpt.springFrameWork.TestA" scope= "singleton" >
<property name= "testB" ref= "testB" />
</bean>
<bean id= "testB" class = "com.chenpt.springFrameWork.TestB" scope= "singleton" >
<property name= "testA" ref= "testA" />
</bean>
</beans>
|
客戶端執行(自行演示,無輸出錯誤)
3. prototype範圍的依賴處理(無法解決)
對於prototype作用域bean,spring容器無法完成依賴注入,因爲spring容器不進行緩存prototype作用域的bean,因此無法提前暴露一個正在創建中的bean。
示例代碼如下:
xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version= "1.0" encoding= "UTF-8" ?>
<beans xmlns= "http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http: //www.springframework.org/schema/beans
http: //www.springframework.org/schema/beans/spring-beans.xsd">
<bean id= "testA" class = "com.chenpt.springFrameWork.TestA" scope= "prototype" >
<property name= "testB" ref= "testB" />
</bean>
<bean id= "testB" class = "com.chenpt.springFrameWork.TestB" scope= "prototype" >
<property name= "testA" ref= "testA" />
</bean>
</beans>
|
客戶端示例
1
2
3
4
5
6
7
|
public class MainTest {
public static void main(String[] args){
ApplicationContext context = new FileSystemXmlApplicationContext( "classpath:spring/applicationContext.xml" );
TestB testB = context.getBean( "testB" ,TestB. class );
}
}
|
執行結果,拋出異常
針對上述的作用域(scope)分析
beanFactory除了擁有ioc的職責外,還有着對象生命週期管理。
scope用來聲明容器中對象所應該處的限定場景或者該對象的存活時間,即容器在對象進入其相應的scope之前,生成並裝配這些對象,在對象不再處於scope的限定之後,容器通常會銷燬這些對象。
spring容器提供了幾種scope類型?
- singleton:在spring容器中只存在一個實例,所有對象的引用將共享這個實例。(注:不要和單例模式搞混)
- prototype:容器每次都會生成一個新的對象實例給請求方。
- request (限定在web應用中使用):爲每個http請求創建一個全新的request-processor對象供當前請求使用,請求結束,實例生命週期即結束。
- session (限定在web應用中使用):爲每個獨立的session創建一個全新的UserPreference對象實例。
- global session (限定在web應用中使用):只有應用在基於portlet的Web應用程序中才有意義,它映射到portlet的global範圍的session。如果在普通的基於servlet的Web應用中使用了這個類型的scope,容器會將其作爲普通的session類型的scope對待。