當(dāng)Spring循環(huán)依賴遇上了BeanPostProcessor動態(tài)代理

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. 解決方案

找到問題原因了,那么問題的解決通常就有了

  1. 項目中盡量避免Spring的循環(huán)引用,這本來就是不合理的。

  2. 使用@Lazy加載機(jī)制來解決

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,665評論 19 139
  • 1.1 Spring IoC容器和bean簡介 本章介紹了Spring Framework實現(xiàn)的控制反轉(zhuǎn)(IoC)...
    起名真是難閱讀 2,675評論 0 8
  • 1.1 spring IoC容器和beans的簡介 Spring 框架的最核心基礎(chǔ)的功能是IoC(控制反轉(zhuǎn))容器,...
    simoscode閱讀 6,852評論 2 22
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,282評論 6 342
  • 看到朋友圈鋪天蓋地的關(guān)于高考的訊息,和來自四面八方對考生的祝福,讓我不禁又想到了我高考那年,不禁又想到那些高中生活...
    不負(fù)桃李閱讀 249評論 0 1

友情鏈接更多精彩內(nèi)容