溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring的單例Bean初始化過程是什么

發(fā)布時(shí)間:2022-03-19 10:23:18 來源:億速云 閱讀:152 作者:iii 欄目:云計(jì)算

本篇內(nèi)容介紹了“Spring的單例Bean初始化過程是什么”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

createBean方法是AbstractBeanFactory的子類AbstractAutowireCapableBeanFactory的一個(gè)方法,看一下它的方法實(shí)現(xiàn):

protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)

        throws BeanCreationException {

 

    if (logger.isDebugEnabled()) {

        logger.debug("Creating instance of bean '" + beanName + "'");

    }

    // Make sure bean class is actually resolved at this point.

    resolveBeanClass(mbd, beanName);

 

    // Prepare method overrides.

    try {

        mbd.prepareMethodOverrides();

    }

    catch (BeanDefinitionValidationException ex) {

        throw new BeanDefinitionStoreException(mbd.getResourceDescription(),

                beanName, "Validation of method overrides failed", ex);

    }

 

    try {

        // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.

        Object bean = resolveBeforeInstantiation(beanName, mbd);

        if (bean != null) {

            return bean;

        }

    }

    catch (Throwable ex) {

        throw new BeanCreationException(mbd.getResourceDescription(), beanName,

                "BeanPostProcessor before instantiation of bean failed", ex);

    }

 

    Object beanInstance = doCreateBean(beanName, mbd, args);

    if (logger.isDebugEnabled()) {

        logger.debug("Finished creating instance of bean '" + beanName + "'");

    }

    return beanInstance;

}

前面的代碼都沒什么意義,代碼執(zhí)行到第31行:

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) {

        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) {

            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.

    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");

        }

        addSingletonFactory(beanName, new ObjectFactory() {

            public Object getObject() throws BeansException {

                return getEarlyBeanReference(beanName, mbd, bean);

            }

        });

    }

 

    // Initialize the bean instance.

    Object exposedObject = bean;

    try {

        populateBean(beanName, mbd, instanceWrapper);

        if (exposedObject != null) {

            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 {

        registerDisposableBeanIfNecessary(beanName, bean, mbd);

    }

    catch (BeanDefinitionValidationException ex) {

        throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);

    }

 

    return exposedObject;

}

代碼跟蹤到這里,已經(jīng)到了主流程,接下來分段分析doCreateBean方法的代碼。

創(chuàng)建Bean實(shí)例

第8行的createBeanInstance方法,會創(chuàng)建出Bean的實(shí)例,并包裝為BeanWrapper,看一下createBeanInstance方法,只貼最后一段比較關(guān)鍵的:

// Need to determine the constructor...

Constructor[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);

if (ctors != null ||

        mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||

        mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args))  {

    return autowireConstructor(beanName, mbd, ctors, args);

}

 

// No special handling: simply use no-arg constructor.

return instantiateBean(beanName, mbd);

意思是bean標(biāo)簽使用構(gòu)造函數(shù)注入屬性的話,執(zhí)行第6行,否則執(zhí)行第10行。MultiFunctionBean使用默認(rèn)構(gòu)造函數(shù),使用setter注入屬性,因此執(zhí)行第10行代碼:

protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {

    try {

        Object beanInstance;

        final BeanFactory parent = this;

        if (System.getSecurityManager() != null) {

            beanInstance = AccessController.doPrivileged(new PrivilegedAction<Object>() {

                public Object run() {

                    return getInstantiationStrategy().instantiate(mbd, beanName, parent);

                }

            }, getAccessControlContext());

        }

        else {

            beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);

        }

        BeanWrapper bw = new BeanWrapperImpl(beanInstance);

        initBeanWrapper(bw);

        return bw;

    }

    catch (Throwable ex) {

        throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);

    }

}

代碼執(zhí)行到13行:

public Object instantiate(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) {

    // Don't override the class with CGLIB if no overrides.

    if (beanDefinition.getMethodOverrides().isEmpty()) {

        Constructor<?> constructorToUse;

        synchronized (beanDefinition.constructorArgumentLock) {

            constructorToUse = (Constructor<?>) beanDefinition.resolvedConstructorOrFactoryMethod;

            if (constructorToUse == null) {

                final Class clazz = beanDefinition.getBeanClass();

                if (clazz.isInterface()) {

                    throw new BeanInstantiationException(clazz, "Specified class is an interface");

                }

                try {

                    if (System.getSecurityManager() != null) {

                        constructorToUse = AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor>() {

                            public Constructor run() throws Exception {

                                return clazz.getDeclaredConstructor((Class[]) null);

                            }

                        });

                    }

                    else {

                        constructorToUse = clazz.getDeclaredConstructor((Class[]) null);

                    }

                    beanDefinition.resolvedConstructorOrFactoryMethod = constructorToUse;

                }

                catch (Exception ex) {

                    throw new BeanInstantiationException(clazz, "No default constructor found", ex);

                }

            }

        }

        return BeanUtils.instantiateClass(constructorToUse);

    }

    else {

        // Must generate CGLIB subclass.

        return instantiateWithMethodInjection(beanDefinition, beanName, owner);

    }

}

整段代碼都在做一件事情,就是選擇一個(gè)使用的構(gòu)造函數(shù)。當(dāng)然第9行順帶做了一個(gè)判斷:實(shí)例化一個(gè)接口將報(bào)錯。

最后調(diào)用到30行,看一下代碼:

public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {

    Assert.notNull(ctor, "Constructor must not be null");

    try {

        ReflectionUtils.makeAccessible(ctor);

        return ctor.newInstance(args);

    }

    catch (InstantiationException ex) {

        throw new BeanInstantiationException(ctor.getDeclaringClass(),

                "Is it an abstract class?", ex);

    }

    catch (IllegalAccessException ex) {

        throw new BeanInstantiationException(ctor.getDeclaringClass(),

                "Is the constructor accessible?", ex);

    }

    catch (IllegalArgumentException ex) {

        throw new BeanInstantiationException(ctor.getDeclaringClass(),

                "Illegal arguments for constructor", ex);

    }

    catch (InvocationTargetException ex) {

        throw new BeanInstantiationException(ctor.getDeclaringClass(),

                "Constructor threw exception", ex.getTargetException());

    }

}

通過反射生成Bean的實(shí)例??吹角懊嬗幸徊絤akeAccessible,這意味著即使Bean的構(gòu)造函數(shù)是private、protected的,依然不影響B(tài)ean的構(gòu)造。

最后注意一下,這里被實(shí)例化出來的Bean并不會直接返回,而是會被包裝為BeanWrapper繼續(xù)在后面使用。

“Spring的單例Bean初始化過程是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI