歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Spring bean是如何加載的

加載bean的主要邏輯

在AbstractBeanFactory中doGetBean對加載bean的不同情況進行拆分處理,並做了部分准備工作
具體如下

  1. 獲取原始bean name
    1. 根據alia獲取原始bean name
    2. 去除FactoryBean時的& [如果是需要獲取FactoryBean自省,配置時需要在bean name前添加&]
  2. 嘗試從緩存中獲取實例
    1. 如果獲取到實例,還要委托getObjectForBeanInstance解決FactoryBean的場景,就是調用getObject
  3. 判斷原型場景的循環依賴問題,如果是原型同時bean又正在創建,說明是循環依賴,那直接拋異常,spring不嘗試解決原型的循環依賴
  4. 如果在本容器中沒有定義該bean,需要去父容器查找
    • 如果有參數,結合參數初始化
    • 如果沒有參數,需要結合類型初始化,這邊的調用是這個分支(當然這邊一樣沒有類型)
  5. 如果不是類型檢查,這邊需要標記bean正在實例化
  6. bean實例化的准備工作
    1. 合並父bean的定義,並轉化GenericBeanDefinition為RootBeanDefinition
    2. 校驗BeanDefinition,如果是抽象類或者非原型帶參數拋異常[這邊注釋說的是只有原型才可以配置構造方法的參數]
    3. 解決bean的依賴
      • 注冊依賴的bean
      • 遞歸調用getBean實例化依賴bean
  7. 創建單例的實例
    • 為解決循環依賴問題,這邊使用ObjectFactory在實例化前先暴露bean
    • 老規矩,需要委托getObejctForBeanInstance解決FactoryBean的問題
  8. 創建原型實例
    • 創建前的准備工作,使用prototypesCurrentlyInCreation標記bean正在實例化
    • 委托createBean實例化bean
    • 創建後的善後工作,從prototypesCurrentlyInCreation中刪除標記
    • 老規矩,委托getObjectForBeanInstance解決工廠方法的問題
  9. 創建其他scope的實例,這邊的邏輯結合了單例跟原型的處理邏輯,即使用解決循環依賴的ObjectFactory也使用prototypeCreation的標記
    • 獲取作用域scope,並校驗是否已配置
    • 使用ObjectFactory提早暴露實例
    • 標記bean正在創建並委托createBean實例化
    • 又是委托getObjectForBeanInstance解決工廠方法問題
  10. 最後需要對創建的實例進行類型校驗,如果不一致,這邊還需要委托TypeConverter進行類型裝換

AbstractBeanFactory

@Override
public Object getBean(String name) throws BeansException {
    return doGetBean(name, null, null, false);
}
protected <T> T doGetBean(
        final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
        throws BeansException {

    // 獲取原始的bean name,去除&,解決alias問題
    final String beanName = transformedBeanName(name);
    Object bean;

    // 嘗試從緩存中獲取bean
    // Eagerly check singleton cache for manually registered singletons.
    Object sharedInstance = getSingleton(beanName);
    if (sharedInstance != null && args == null) {
        if (logger.isDebugEnabled()) {
            if (isSingletonCurrentlyInCreation(beanName)) {
                // ...
            }
            else {
                logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
            }
        }
        // 如果從緩存中或得bean,還需要判斷是否是FactoryBean,並調用getObejct
        bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
    }

    else {
        // 如果是原型scope,這邊又是正在創建,說明有循環依賴,而原型的循環依賴Spring是不解決的
        // Fail if we're already creating this bean instance:
        // We're assumably within a circular reference.
        if (isPrototypeCurrentlyInCreation(beanName)) {
            throw new BeanCurrentlyInCreationException(beanName);
        }

        // 如果當前容器沒有配置bean,那麼去父容器查找
        // Check if bean definition exists in this factory.
        BeanFactory parentBeanFactory = getParentBeanFactory();
        if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
            // Not found -> check parent.
            String nameToLookup = originalBeanName(name);
            if (args != null) {
                // Delegation to parent with explicit args.
                return (T) parentBeanFactory.getBean(nameToLookup, args);
            }
            else {
                // No args -> delegate to standard getBean method.
                return parentBeanFactory.getBean(nameToLookup, requiredType);
            }
        }

        // 如果不是類型檢查,這邊需要標記類正在創建
        if (!typeCheckOnly) {
            markBeanAsCreated(beanName);
        }

        try {
            // 實例化類之前,先去容器中獲取配置的bean信息,這邊需要將之前的GenericBeanDefinition轉化為RootBeanDefinition
            // 同時如果父bean的話,需要合並到子bean
            final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
            checkMergedBeanDefinition(mbd, beanName, args);

            // Guarantee initialization of beans that the current bean depends on.
            String[] dependsOn = mbd.getDependsOn();
            if (dependsOn != null) {
                for (String dependsOnBean : dependsOn) {
                    if (isDependent(beanName, dependsOnBean)) {
                        throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                "Circular depends-on relationship between '" + beanName + "' and '" + dependsOnBean + "'");
                    }
                    // 解決依賴
                    registerDependentBean(dependsOnBean, beanName);
                    getBean(dependsOnBean);
                }
            }

            // 創建單例的實例
            // Create bean instance
            if (mbd.isSingleton()) {
                // 單例情況下,為解決循環依賴,在實例化之前,先新建一個ObjectFactory實例
                sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
                    @Override
                    public Object getObject() throws BeansException {
                        try {
                            return createBean(beanName, mbd, args);
                        }
                        catch (BeansException ex) {
                            // Explicitly remove instance from singleton cache: It might have been put there
                            // eagerly by the creation process, to allow for circular reference resolution.
                            // Also remove any beans that received a temporary reference to the bean.
                            destroySingleton(beanName);
                            throw ex;
                        }
                    }
                });
                bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
            }

            // 創建原型實例
            else if (mbd.isPrototype()) {
                // It's a prototype -> create a new instance.
                Object prototypeInstance = null;
                try {
                    beforePrototypeCreation(beanName);
                    prototypeInstance = createBean(beanName, mbd, args);
                }
                finally {
                    afterPrototypeCreation(beanName);
                }
                bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
            }

            // 創建其他scope的實例
            else {
                String scopeName = mbd.getScope();
                final Scope scope = this.scopes.get(scopeName);
                if (scope == null) {
                    throw new IllegalStateException("No Scope registered for scope '" + scopeName + "'");
                }
                try {
                    // 還是先創建ObejctFactory,只是這邊沒有處理
                    Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
                        @Override
                        public Object getObject() throws BeansException {
                            beforePrototypeCreation(beanName);
                            try {
                                return createBean(beanName, mbd, args);
                            }
                            finally {
                                afterPrototypeCreation(beanName);
                            }
                        }
                    });
                    bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
                }
                catch (IllegalStateException ex) {
                    throw new BeanCreationException(beanName,
                            "Scope '" + scopeName + "' is not active for the current thread; " +
                            "consider defining a scoped proxy for this bean if you intend to refer to it from a singleton",
                            ex);
                }
            }
        }
        catch (BeansException ex) {
            cleanupAfterBeanCreationFailure(beanName);
            throw ex;
        }
    }

    // 這邊需要對實例進行類型校驗,如果與requiredType不一致,需要委托TypeConverter嘗試類型轉換
    // Check if required type matches the type of the actual bean instance.
    if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) {
        try {
            return getTypeConverter().convertIfNecessary(bean, requiredType);
        }
        catch (TypeMismatchException ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to convert bean '" + name + "' to required type [" +
                        ClassUtils.getQualifiedName(requiredType) + "]", ex);
            }
            throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
        }
    }
    return (T) bean;
}

bean實例的緩存分析

上面提到在加載bean的時候,doGetBean首先嘗試的是從緩存讀取,這邊我們來細細分析下緩存具體是如何處理的.

這邊邏輯是定義在DefaultSingletonBeanRegistry中,它是AbstractBeanFactory的父類,主要職責是共享實例的注冊.
這邊雖然定義的是singleton,但是實際使用的時候,處理prototype,其他scope均使用了這邊進行緩存.

這邊主要是需要理解singletonObjects,singletonFactories,earlySingletonObjects,registeredSingletons這4個變量.

singletonObjects 緩存bean name ->實例

Cache of singleton objects: bean name --> bean instance
這邊緩存的是實例

singletonFactories 緩存bean name -->ObjectFactory

Cache of singleton factories: bean name --> ObjectFactory
這邊緩存的是為解決循環依賴而准備的ObjectFactory

earlySingletonObjects 緩存提早暴露的實例 bean name -->bean instance

Cache of early singleton objects: bean name --> bean instance
這邊緩存的也是實例,只是這邊的是為解決循環依賴而提早暴露出來的實例,其實是ObjectFactory

registeredSingletons 已經注冊的單例bean name

Set of registered singletons, containing the bean names in registration order
上面三個變量,任意一個添加了,這邊都會添加bean name,標記已經注冊

4個變量的關系如下:

  • singletonObjects與singletonFactories,earlySingletonObjects,是互斥的.就是一個bean如果在其中任意一個變量中就,不會存在在另一變量中.這三個變量用於記錄一個bean的不同狀態.
  • 如果bean已經添加到singletonObjects中,那麼singltonFactories和earlySinletonObjects都不會考慮
  • singltonFactories中的bean 通過 ObjectFactory的getObject實例化後,添加到earlySingletonObjects

我們從下面幾個方法,可以清楚看懂上面4個變量的使用:
DefaultSingletonBeanRegistry

/** * 添加實例化的bean * Add the given singleton object to the singleton cache of this factory. * <p>To be called for eager registration of singletons. * @param beanName the name of the bean * @param singletonObject the singleton object */
protectedvoidaddSingleton(String beanName, Object singletonObject) {
    synchronized (this.singletonObjects) {
        this.singletonObjects.put(beanName, (singletonObject != null ? singletonObject : NULL_OBJECT));
        this.singletonFactories.remove(beanName);
        this.earlySingletonObjects.remove(beanName);
        this.registeredSingletons.add(beanName);
    }
}

/** * 為解決單例的循環依賴,這邊注冊ObjectFactory * Add the given singleton factory for building the specified singleton * if necessary. * <p>To be called for eager registration of singletons, e.g. to be able to * resolve circular references. * @param beanName the name of the bean * @param singletonFactory the factory for the singleton object */
protectedvoidaddSingletonFactory(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);
        }
    }
}
/** * 清除實例 * Remove the bean with the given name from the singleton cache of this factory, * to be able to clean up eager registration of a singleton if creation failed. * @param beanName the name of the bean * @see #getSingletonMutex() */
protectedvoidremoveSingleton(String beanName) {
    synchronized (this.singletonObjects) {
        this.singletonObjects.remove(beanName);
        this.singletonFactories.remove(beanName);
        this.earlySingletonObjects.remove(beanName);
        this.registeredSingletons.remove(beanName);
    }
}
/** * 獲取實例時,調用ObejctFactory的getObject 獲取實例 * Return the (raw) singleton object registered under the given name. * <p>Checks already instantiated singletons and also allows for an early * reference to a currently created singleton (resolving a circular reference). * @param beanName the name of the bean to look for * @param allowEarlyReference whether early references should be created or not * @return the registered singleton object, or {@code null} if none found */
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();
                    this.earlySingletonObjects.put(beanName, singletonObject);
                    this.singletonFactories.remove(beanName);
                }
            }
        }
    }
    return (singletonObject != NULL_OBJECT ? singletonObject : null);
}

Spring中如何配置Hibernate事務 http://www.linuxidc.com/Linux/2013-12/93681.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

基於 Spring 設計並實現 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm

Spring-3.2.4 + Quartz-2.2.0集成實例 http://www.linuxidc.com/Linux/2013-10/91524.htm

使用 Spring 進行單元測試 http://www.linuxidc.com/Linux/2013-09/89913.htm

運用Spring注解實現Netty服務器端UDP應用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm

Spring 3.x 企業應用開發實戰 PDF完整高清掃描版+源代碼 http://www.linuxidc.com/Linux/2013-10/91357.htm

Spring 的詳細介紹:請點這裡
Spring 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved