1. 程式人生 > >spring IOC之篇六 bean的加載---bean的創建

spring IOC之篇六 bean的加載---bean的創建

bject prot sid share dha pri family cannot ger

之前我們講解了緩存中單例的獲取,解下來需要講解緩存中不存在該如何處理

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
        Assert.notNull(beanName, "‘beanName‘ must not be null");
        // 全局變量需要同步
        synchronized (this.singletonObjects) {
           // 先從單例緩存池獲取,以便於復用
            Object singletonObject = this
.singletonObjects.get(beanName); if (singletonObject == null) { // 如果為空,才進行單例bean的初始化 if (this.singletonsCurrentlyInDestruction) { throw new BeanCreationNotAllowedException(beanName, "Singleton bean creation not allowed while the singletons of this factory are in destruction " + "(Do not request a bean from a BeanFactory in a destroy method implementation!)"); }
if (logger.isDebugEnabled()) { logger.debug("Creating shared instance of singleton bean ‘" + beanName + "‘"); } //記錄加載狀態,this.singletonsCurrentlyInCreation.add(beanName),把正在創建的Bean加入到緩存中,可以對循環依賴進行檢測 beforeSingletonCreation(beanName);
boolean newSingleton = false; boolean recordSuppressedExceptions = (this.suppressedExceptions == null); if (recordSuppressedExceptions) { this.suppressedExceptions = new LinkedHashSet<Exception>(); } try { //初始化bean singletonObject = singletonFactory.getObject(); newSingleton = true; } catch (IllegalStateException ex) { // Has the singleton object implicitly appeared in the meantime -> // if yes, proceed with it since the exception indicates that state. singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null) { throw ex; } } catch (BeanCreationException ex) { if (recordSuppressedExceptions) { for (Exception suppressedException : this.suppressedExceptions) { ex.addRelatedCause(suppressedException); } } throw ex; } finally { if (recordSuppressedExceptions) { this.suppressedExceptions = null; } // 移除緩存中正在加載bean的狀態 afterSingletonCreation(beanName); } if (newSingleton) { // 將結果記錄值緩存,並刪除所有的輔助狀態 addSingleton(beanName, singletonObject); } } return (singletonObject != NULL_OBJECT ? singletonObject : null); } }

接下來對代碼 singletonObject = this.singletonObjects.get(beanName);進行分析

我們跟蹤了這麽久的代碼發現,一個真正幹活的代碼一般是以do開頭的,比如 doGetObejctFactoryBean,而給我們錯覺的函數,比如getObejctFactoryBean 只是做一些全局的統籌工作,這個規則對createBean也不列外。

@Override
    protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
        if (logger.isDebugEnabled()) {
            logger.debug("Creating instance of bean ‘" + beanName + "‘");
        }
        RootBeanDefinition mbdToUse = mbd;

        // Make sure bean class is actually resolved at this point, and
        // clone the bean definition in case of a dynamically resolved Class
        // which cannot be stored in the shared merged bean definition.
       // 根據設置的class來解析class
        Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
        if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
            mbdToUse = new RootBeanDefinition(mbd);
            mbdToUse.setBeanClass(resolvedClass);
        }

        // Prepare method overrides.
        try {
        // 對 override-method進行驗證以及標記,其實是指的lookup-method和replace-method屬性的配置相關的信息,是統一放到BeanDefinition的methodOverrides屬性裏面
            mbdToUse.prepareMethodOverrides();
        }
        catch (BeanDefinitionValidationException ex) {
            throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
                    beanName, "Validation of method overrides failed", ex);
        }

        try {
            // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.     // 應用初始化後處理器,解析是否存在初始化前的短路操作,如果是AOP則,bean不為空,直接返回
           
Object bean = resolveBeforeInstantiation(beanName, mbdToUse); if (bean != null) { return bean; } } catch (Throwable ex) { throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName, "BeanPostProcessor before instantiation of bean failed", ex); } //創建bean Object beanInstance = doCreateBean(beanName, mbdToUse, args); if (logger.isDebugEnabled()) { logger.debug("Finished creating instance of bean ‘" + beanName + "‘"); } return beanInstance; }

進一步的對createBean進行分析

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {
        // Instantiate the bean.
        BeanWrapper instanceWrapper = null;
        if (mbd.isSingleton()) {
            instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
        }
        if (instanceWrapper == null) {
          // 將BeanDefinition轉換為BeanWrapper,以便於在裝配的時候把屬性值都賦值到BeanWrapper對象裏面,BeanWrapper實現了屬性編輯器接口,會把默認對象的屬性編輯對象註冊到PropertyEditorRegistrySupport對象的屬性中去,方便遇到對應的屬性值的進行解析
            instanceWrapper = createBeanInstance(beanName, mbd, args);
        }
        final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
        Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);

        // Allow post-processors to modify the merged bean definition.
        synchronized (mbd.postProcessingLock) {
            if (!mbd.postProcessed) {
            // bean合並後的處理,Autowried正是基於此註解實現了類型的預解析
                applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
                mbd.postProcessed = true;
            }
        }

        // Eagerly cache singletons to be able to resolve circular references
        // even when triggered by lifecycle interfaces like BeanFactoryAware.
        // spring的循環依賴問題的解決,當A中含有B屬性時候,而B中有含有A屬性時候就會構成一個循環依賴,此時如果A和B都是單列,那麽Spring的處理方式就是當創建B的時候,如果涉及到A,則不去創建A而是通過放入緩存的ObectFactory來創建實例,因為屬性的地址是一樣的,這樣就解決了循環依賴的問題。
        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");
            }
 // 避免後期循環依賴,將bean的ObjectFactory對象加入到緩存中去
            addSingletonFactory(beanName, new ObjectFactory<Object>() {
                @Override
                public Object getObject() throws BeansException {
                    return getEarlyBeanReference(beanName, mbd, bean);
                }
            });
        }

        // Initialize the bean instance.
        Object exposedObject = bean;
        try {
           // 對bean進行裝配,將各個屬性值賦值註入,可以依賴其他bean則遞歸調用初始依賴bean
            populateBean(beanName, mbd, instanceWrapper);
            if (exposedObject != null) {
                // 調用初始化方法,如:init-method 如果實現了接口 InitializingBean,則會先調用afterPropertiesSet方法 之後會調用init-method方法
                exposedObject = initializeBean(beanName, exposedObject, mbd);
            }
        }
        catch (Throwable ex) {
            if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
                throw (BeanCreationException) ex;
            }
            else {
                throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
            }
        }
       // 循環依賴檢查,非單例情況下面,直接拋出異常
        if (earlySingletonExposure) {
            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.");
                    }
                }
            }
        }

        // Register bean as disposable.
        try {
         // 根據scope註冊bean,包括銷毀方法的註冊 destory-method方法 
            registerDisposableBeanIfNecessary(beanName, bean, mbd);
        }
        catch (BeanDefinitionValidationException ex) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
        }

        return exposedObject;
    }

spring IOC之篇六 bean的加載---bean的創建