本文是直接摘抄《Spring源碼深度解析》5.6節(jié)循環(huán)依賴泛豪,首先是加深自己的理解,其次是方便查閱侦鹏。
什么是循環(huán)依賴
循環(huán)依賴就是循環(huán)引用诡曙,就是兩個(gè)或多個(gè)bean相互之間持有對方,比如CircleA引用CircleB略水,circleB引用CircleC岗仑,CircleC引用CircleA,則它們最終反映為一個(gè)環(huán)聚请。此處不是循環(huán)調(diào)用,循環(huán)調(diào)用是方法之間的調(diào)用稳其,如下圖所示驶赏。
循環(huán)調(diào)用是無法解決的,除非有終結(jié)條件既鞠,否則就是死循環(huán)煤傍,最終導(dǎo)致內(nèi)存溢出錯(cuò)誤。
Spring如何解決循環(huán)依賴
Spring容器循環(huán)依賴包括構(gòu)造器循環(huán)依賴和setter循環(huán)依賴嘱蛋,那么Spring容器如何解決循環(huán)依賴呢蚯姆?首先讓我們來定義循環(huán)引用類:
class TestA {
private TestB testB;
public void setTestB(TestB testB) {
this.testB = testB;
}
}
class TestB {
private TestC testC;
public void setTestC(TestC testC) {
this.testC = testC;
}
}
class TestC {
private TestA testA;
public void setTestA(TestA testA) {
this.testA = testA;
}
}
如何是我們自己硬編碼五续,會怎么處理這種循環(huán)依賴,我們首先會把所有的對象都創(chuàng)建出來龄恋,然后再設(shè)置值疙驾。
TestA testA = new TestA();
TestB testB = new TestB();
TestC testC = new TestC();
testA.setTestB(testB);
testB.setTestC(testC);
testC.setTestA(testA);
在Spring中將循環(huán)依賴的處理分為3中情況。
- 構(gòu)造器循環(huán)依賴
表示通過構(gòu)造器注入構(gòu)成的循環(huán)依賴郭毕,此依賴是無法解決的它碎,只能拋出BeanCurrentlyInCreationException異常表示循環(huán)依賴。
如在創(chuàng)建TestA類時(shí)显押,構(gòu)造器需要TestB類扳肛,那將去創(chuàng)建TestB,在創(chuàng)建TestB類時(shí)又發(fā)現(xiàn)需要TestC類乘碑,則又去創(chuàng)建TestC挖息,最終在創(chuàng)建TestC時(shí)又需要TestA,從而形成一個(gè)環(huán)兽肤,沒辦法創(chuàng)建套腹。 - setter循環(huán)依賴
表示通過setter注入方式構(gòu)成的循環(huán)依賴。對于setter注入造成的依賴是通過Spring容器提前暴露剛初始化完但未完成其他步驟(如setter注入)的bean來完成轿衔,而且只能解決單例作用域的bean循環(huán)依賴沉迹。從而使其他bean能引用到該bean,如下代碼所示:(具體邏輯見第三節(jié))
addSingletonFactory(beanName, new ObjectFactory<Object>() {
@Override
public Object getObject() throws BeansException {
return getEarlyBeanReference(beanName, mbd, bean);
}
});
- prototype范圍的依賴處理
對于"prototype"作用域bean害驹,Spring容器無法完成依賴注入鞭呕,因?yàn)镾pring容器不進(jìn)行換成"prototype"作用域的bean,因?yàn)闊o法提前暴露一個(gè)創(chuàng)建中的bean宛官。
setter循環(huán)依賴實(shí)現(xiàn)原理
Spring容器對單例bean創(chuàng)建定義了幾種不同的狀態(tài)葫松,并緩存中不同的Map中,Map的key值都是beanName底洗。
/** Cache of singleton objects: bean name --> bean instance */
//緩存已經(jīng)創(chuàng)建好的bean
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);
/** Cache of singleton factories: bean name --> ObjectFactory */
//有循環(huán)依賴時(shí)腋么,在bean還沒完全創(chuàng)建時(shí),提前暴露ObjectFcatory亥揖,ObjectFactory能夠獲取bean珊擂。
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<String, ObjectFactory<?>>(16);
/** Cache of early singleton objects: bean name --> bean instance */
//bean還沒完全創(chuàng)建成功,緩存從ObjectFactory中獲取的bean费变,earlySingletonObjects和singletonFactories只能其中一個(gè)緩存bean摧扇。
private final Map<String, Object> earlySingletonObjects = new HashMap<String, Object>(16);
或許現(xiàn)在還是沒有明白singletonFactories和earlySingletonObjects是干嘛的,我們繼續(xù)往下看挚歧,DefaultSingletonBeanRegistry#getSingleton(String beanName, boolean allowEarlyReference)方法扛稽,這方法會在創(chuàng)建bean的時(shí)候調(diào)用。
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
//bean實(shí)例是否已經(jīng)創(chuàng)建完成
Object singletonObject = this.singletonObjects.get(beanName);
//若沒有創(chuàng)建完成滑负,是否正在創(chuàng)建
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
//early singleton是否存在
singletonObject = this.earlySingletonObjects.get(beanName);
//若不存在在张,allowEarlyReference傳進(jìn)來true
if (singletonObject == null && allowEarlyReference) {
//從singleton factories獲取ObjectFactory用含,并調(diào)用getObject()
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
//把bean添加到earlySingletonObjects中,并從singletonFactories移除
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}
isSingletonCurrentlyInCreation(beanName):這個(gè)方法表示該bean是否在創(chuàng)建中帮匾。在Spring中啄骇,會有個(gè)專門的屬性默認(rèn)為DefaultSingletonBeanRegistry的singletonsCurrentlyInCreation來記錄bean的加載狀態(tài),在bean創(chuàng)建前會將beanName記錄在屬性中辟狈,在bean創(chuàng)建結(jié)束后會將beanName從屬性中移除肠缔。在singleton下記錄屬性的函數(shù)是在DefaultSingletonBeanRegistry類的public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory)函數(shù)的beforeSingletonCreation(beanName)和afterSingletonCreation(beanName)中。
最后我們再來關(guān)注singletonFactories是在什么時(shí)候添加進(jìn)去的哼转?
在AbstractAutowireCapableBeanFactory#doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)有下面一段代碼:
// Eagerly cache singletons to be able to resolve circular references
// even when triggered by lifecycle interfaces like BeanFactoryAware.
//如果bean是單例的明未,并且允許循環(huán)依賴,并且正在創(chuàng)建壹蔓,則調(diào)用addSingletonFactory
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
if (logger.isDebugEnabled()) {
logger.debug("Eagerly caching bean '" + beanName +
"' to allow for resolving potential circular references");
}
//創(chuàng)建匿名ObjectFactory趟妥,并把ObjectFactory緩存到singletonFactories中
addSingletonFactory(beanName, new ObjectFactory<Object>() {
@Override
public Object getObject() throws BeansException {
//獲取bean實(shí)例
return getEarlyBeanReference(beanName, mbd, bean);
}
});
}
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(singletonFactory, "Singleton factory must not be null");
synchronized (this.singletonObjects) {
if (!this.singletonObjects.containsKey(beanName)) {
this.singletonFactories.put(beanName, singletonFactory);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
}
我在看getEarlyBeanReference(beanName, mbd, bean)方法時(shí),在想匿名ObjectFactory類是如何把beanName, mbd, bean這幾個(gè)值給保存起來的佣蓉,以前使用一般都會是在addSingletonFactory()方法中直接調(diào)用ObjectFactory的getObject方法披摄,但是這里是緩存到Map中給以后調(diào)用,具體原理反編譯之后就明白了勇凭,具體參考我另外一篇文章(明天補(bǔ)上).