1.什么是循環(huán)依賴
假設(shè)Spring容器中有兩個Bean:A和B
依賴關(guān)系如下:
A->B->A
@Component
public class CircularA {
@Autowired
private CircularB b;
public CircularA() {
}
public void setB(CircularB b) {
this.b = b;
}
}
@Component
public class CircularB {
@Autowired
private CircularA a;
public CircularB() {
}
public void setA(CircularA a) {
this.a = a;
}
}
Spring容器在創(chuàng)建BeanA的時候,發(fā)現(xiàn)需要依賴BeanB,那么在創(chuàng)建BeanB的時候,發(fā)現(xiàn)需要依賴BeanA,如此就形成循環(huán)依賴。
2. Spring怎么解決循環(huán)依賴
在網(wǎng)上有很多相關(guān)的博客解釋Spring如何解決循環(huán)依賴Spring解決循環(huán)依賴。
簡而言之就是Spring通過三級緩存來解決循環(huán)依賴。在Spring容器初始化過程中,通過beanName獲取Bean的優(yōu)先級依次是:一級緩存->二級緩存->三級緩存
/** Cache of singleton objects: bean name --> bean instance */
/** 一級緩存 */
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);
/** Cache of singleton factories: bean name --> ObjectFactory */
/** 三級緩存 */
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<String, ObjectFactory<?>>(16);
/** Cache of early singleton objects: bean name --> bean instance */
/** 三級緩存 */
private final Map<String, Object> earlySingletonObjects = new HashMap<String, Object>(16);
當(dāng)BeanFactory實例化BeanA后,BeanFactory會把剛剛實例化還沒有依賴注入的Bean包裝成一個ObjectFactory對象放入到三級緩存中,并且從二級緩存中移除,代碼如下
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);
}
}
}
接下來進(jìn)行依賴注入,如果存在循環(huán)依賴,例如A->B->A的情況,A實例化完畢,注入A.b的時候,要實例化B,發(fā)現(xiàn)依賴a,這個時候就要從BeanFactory中獲取a實例,這個時候,緩存升級了。下面方法的第二個參數(shù)是true
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
/** 將獲取到的Bean從三級緩存中移除,并且升級到二級緩存中 */
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}
b實例自己先完成實例化和依賴注入(這個時候a實例只是剛剛實例化,但是已經(jīng)可以滿足beanB的需求了)以及初始化等聲明周期,最后在返回到a的創(chuàng)建流程中,a實例就可以注入已經(jīng)成熟的b實例,a實例自身也順利完成創(chuàng)建,由于b實例持有了a實例的引用,所以在后續(xù)的使用中是完全沒有問題的。
如果Spring中不存在Bean的循環(huán)依賴,應(yīng)該是不存在從三級緩存升級到二級緩存的場景,因為Spring是單線程初始化的。
這樣Spring解決Bean循環(huán)依賴的問題?。?!
3. BeanPostProcessor
BeanPostProcessor接口是用來對bean進(jìn)行后置處理的,這個時候bean已經(jīng)完成實例化和依賴注入了,屬于bean初始化生命周期的一部分。
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
invokeAwareMethods(beanName, bean);
return null;
}
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
如果在BeanPostProcessor的接口中,對傳入的bean進(jìn)行了處理導(dǎo)致返回的bean和傳入的bean不是同一個bean,這個正常情況是沒有問題,很多中間件都是這么做的
但是?。?!當(dāng)Spring 循環(huán)依賴遇上BeanProcessor返回一個不一致對象的時候,就會發(fā)生問題了?。?!
4. 異常情況
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'xxx': Bean with name 'xxx' has been injected into other beans [a,b,c] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
問題的描述就是這個樣子了,大致的意思就是xxx這個bean已經(jīng)注入到很多bean中了,只不過呢依賴xxx的bean中引用的不是它的最終版本,因為他們之間存在循環(huán)依賴的問題,在解決循環(huán)依賴中使用的是二級緩存中的early bean,而解決完循環(huán)依賴后,bean的引用發(fā)生了變化,導(dǎo)致了early bean和 expose bean不相等,所以拋出異常了?。?!
Object earlySingletonReference = getSingleton(beanName, false);
if (earlySingletonReference != null) {
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
String[] dependentBeans = getDependentBeans(beanName);
Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length);
for (String dependentBean : dependentBeans) {
if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
actualDependentBeans.add(dependentBean);
}
}
if (!actualDependentBeans.isEmpty()) {
throw new BeanCurrentlyInCreationException(beanName,
"Bean with name '" + beanName + "' has been injected into other beans [" +
StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
"] in its raw version as part of a circular reference, but has eventually been " +
"wrapped. This means that said other beans do not use the final version of the " +
"bean. This is often the result of over-eager type matching - consider using " +
"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
}
}
}
5. 解決方案
找到問題原因了,那么問題的解決通常就有了
項目中盡量避免Spring的循環(huán)引用,這本來就是不合理的。
使用@Lazy加載機(jī)制來解決