doCreateBean - createBeanInstance

来源:https://www.cnblogs.com/elvinle/archive/2020/07/27/13366438.html
-Advertisement-
Play Games

接著前面, 看完構造函數前的後置處理器, 就到 doCreateBean 方法了. protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] ...


接著前面, 看完構造函數前的後置處理器, 就到 doCreateBean 方法了.

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
            throws BeanCreationException {

    // Instantiate the bean.
    BeanWrapper instanceWrapper = null;
    if (mbd.isSingleton()) {
        instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
    }
    if (instanceWrapper == null) {
        /**
         * 創建 bean 實例,並將實例包裹在 BeanWrapper 實現類對象中返回。
         * createBeanInstance中包含三種創建 bean 實例的方式:
         *   1. 通過工廠方法創建 bean 實例
         *   2. 通過構造方法自動註入(autowire by constructor)的方式創建 bean 實例
         *   3. 通過無參構造方法方法創建 bean 實例
         *
         * 若 bean 的配置信息中配置了 lookup-method 和 replace-method,則會使用 CGLIB 增強 bean 實例。
         */
        instanceWrapper = createBeanInstance(beanName, mbd, args);
    }
    final Object bean = instanceWrapper.getWrappedInstance();
    Class<?> beanType = instanceWrapper.getWrappedClass();
    if (beanType != NullBean.class) {
        mbd.resolvedTargetType = beanType;
    }

    // Allow post-processors to modify the merged bean definition.
    synchronized (mbd.postProcessingLock) {
        if (!mbd.postProcessed) {
            try {
                //調用屬性合併後置處理器, 進行屬性合併
                //這裡會進行 一些註解 的掃描
                //CommonAnnotationBeanPostProcessor -> @PostConstruct @PreDestroy @Resource
                //AutowiredAnnotationBeanPostProcessor -> @Autowired @Value
                applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
            }
            catch (Throwable ex) {
                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                        "Post-processing of merged bean definition failed", ex);
            }
            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.isTraceEnabled()) {
            logger.trace("Eagerly caching bean '" + beanName +
                    "' to allow for resolving potential circular references");
        }
        //這裡創建了一個匿名的 ObjectFactory 實現類, 他是一個工廠, 可以用來獲取對象
        //addSingletonFactory中, 將這個工廠放到 singletonFactories 中去了. singletonFactories 是spring的三級緩存
        addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
    }

    // Initialize the bean instance.
    Object exposedObject = bean;
    try {
        //設置屬性,非常重要
        populateBean(beanName, mbd, instanceWrapper);
        //執行後置處理器,aop就是在這裡完成的處理
        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<>(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;
}

1. 第一次進來時, 對象肯定是null, 所以會進行一次反射創建過程.

2. 然後會執行屬性合併後置處理器, 在這個後置處理器中, 會進行一些屬性合併和一些註解的掃描

3. 合併完屬性之後, 就要進行屬性的註入了, 比如 @Autowired 註入

4. 開始初始化過程, 調用 一些初始化方法

 

整個過程是非常符合邏輯的.

沒有對象則創建對象, 有了對象之後, 看看有那些屬性需要註入, 當屬性都準備妥當時, 執行初始化方法, 比如對這些屬性做一些什麼操作或者通過這些屬性, 拿到一些什麼東西等等.

 

createBeanInstance

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBeanInstance

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
    // Make sure bean class is actually resolved at this point.
    Class<?> beanClass = resolveBeanClass(mbd, beanName);

    //檢測一個類的訪問許可權, spring預設情況下對於非public的類是允許訪問的。
    if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
        throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                "Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
    }

    Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
    if (instanceSupplier != null) {
        return obtainFromSupplier(instanceSupplier, beanName);
    }


    // 如果工廠方法不為空,則通過工廠方法構建 bean 對象
    // 這種是在 xml 中配置 bean 的 factory-method
    if (mbd.getFactoryMethodName() != null) {
        return instantiateUsingFactoryMethod(beanName, mbd, args);
    }

    // Shortcut when re-creating the same bean...
    /**
     * 從spring的原始註釋可以知道這個是一個Shortcut,什麼意思呢?
     * 當多次構建同一個 bean 時,可以使用這個Shortcut,
     * 也就是說不在需要次推斷應該使用哪種方式構造bean
     *  比如在多次構建同一個prototype類型的 bean 時,就可以走此處的shortcut
     * 這裡的 resolved 和 mbd.constructorArgumentsResolved 將會在 bean 第一次實例
     * 化的過程中被設置
     */
    boolean resolved = false;
    //自動裝配
    boolean autowireNecessary = false;
    if (args == null) {
        synchronized (mbd.constructorArgumentLock) {
            if (mbd.resolvedConstructorOrFactoryMethod != null) {
                resolved = true;
                //如果已經解析了構造方法的參數,則必須要通過一個帶參構造方法來實例
                autowireNecessary = mbd.constructorArgumentsResolved;
            }
        }
    }
    if (resolved) {
        if (autowireNecessary) {
            // 通過構造方法自動裝配的方式構造 bean 對象
            return autowireConstructor(beanName, mbd, null, null);
        }
        else {
            //通過預設的無參構造方法進行
            return instantiateBean(beanName, mbd);
        }
    }

    // Candidate constructors for autowiring?
    // 由後置處理器決定返回哪些構造方法, 當出現多個構造函數時, 返回為 null
    // 這裡的後置處理器是 SmartInstantiationAwareBeanPostProcessor, 調用其 determineCandidateConstructors 方法
    // 具體處理在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor 中進行
    Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
    if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
            mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
        //需要對構造函數的參數進行註入, 可以通過 ImportBeanDefinitionRegistrar 來修改 bd 的自動裝配模式:
        // AnnotatedGenericBeanDefinition bd = new AnnotatedGenericBeanDefinition(IndexDao1.class);
        // bd.setAutowireMode(AnnotatedGenericBeanDefinition.AUTOWIRE_CONSTRUCTOR);
        // registry.registerBeanDefinition("indexDao1", bd);
        return autowireConstructor(beanName, mbd, ctors, args);
    }

    // Preferred constructors for default construction?
    ctors = mbd.getPreferredConstructors();
    if (ctors != null) {
        return autowireConstructor(beanName, mbd, ctors, null);
    }

    // No special handling: simply use no-arg constructor.
    //使用預設的無參構造方法進行初始化
    return instantiateBean(beanName, mbd);
}

我通常的使用習慣是, 使用 @Autowired 來進行註入操作(雖然有些人提倡通過構造註入的方式). 

按照我這個習慣, 構造函數是預設構造函數, actors 返回來是null , 所以不會走 autowireConstructor 來創建,

而是會走到 最下麵, 在 instantiateBean() 中, 通過無參構造函數反射創建對象. 

這種創建對象的方式, 是最簡單的. 同時能避免一個 構造函數造成的相互依賴問題, 這個問題是無解的.

 

determineConstructorsFromBeanPostProcessors

這裡會調用構造函數的後置處理器, 來進行構造函數的選擇

@Nullable
protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable Class<?> beanClass, String beanName)
        throws BeansException {

    if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
                SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
                Constructor<?>[] ctors = ibp.determineCandidateConstructors(beanClass, beanName);
                if (ctors != null) {
                    return ctors;
                }
            }
        }
    }
    return null;
}

這個後置處理器, 只能放在下一篇來看了, 這裡先不管了. 其中的道道, 還是蠻多的.

 

instantiateBean

protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
    try {
        Object beanInstance;
        final BeanFactory parent = this;
        if (System.getSecurityManager() != null) {
            beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () ->
                    getInstantiationStrategy().instantiate(mbd, beanName, parent),
                    getAccessControlContext());
        }
        else {
            //getInstantiationStrategy()得到類的實例化策略
            //得到一個反射的實例化策略 : SimpleInstantiationStrategy
            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);
    }
}

這裡就是實例化 bean 的地方了, 

實例化之後, 還會設置一些屬性轉換器

 

SimpleInstantiationStrategy.instantiate

@Override
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
    // Don't override the class with CGLIB if no overrides.
    //檢測 bean 配置中是否配置了 lookup-method 或 replace-method
    //如果配置了就需使用 CGLIB 構建 bean 對象
    if (!bd.hasMethodOverrides()) {
        Constructor<?> constructorToUse;
        synchronized (bd.constructorArgumentLock) {
            constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
            if (constructorToUse == null) {
                final Class<?> clazz = bd.getBeanClass();
                if (clazz.isInterface()) {
                    throw new BeanInstantiationException(clazz, "Specified class is an interface");
                }
                try {
                    if (System.getSecurityManager() != null) {
                        constructorToUse = AccessController.doPrivileged(
                                (PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);
                    }
                    else {
                        constructorToUse = clazz.getDeclaredConstructor();
                    }
                    bd.resolvedConstructorOrFactoryMethod = constructorToUse;
                }
                catch (Throwable ex) {
                    throw new BeanInstantiationException(clazz, "No default constructor found", ex);
                }
            }
        }
        return BeanUtils.instantiateClass(constructorToUse);
    }
    else {
        // Must generate CGLIB subclass.
        return instantiateWithMethodInjection(bd, beanName, owner);
    }
}

BeanUtils.instantiateClass 就是反射創建對象的方法.

這裡雖說有創建 cglib 的方法, 但是裡面只是拋出了異常

throw new UnsupportedOperationException("Method Injection not supported in SimpleInstantiationStrategy");

lookup-method 和 replace-method , 我基本都只是在學習的時候, 才去使用過, 如果想要用的話, 需要改變這裡的實例化策略, 也就是不用 SimpleInstantiationStrategy .

 

initBeanWrapper

org.springframework.beans.factory.support.AbstractBeanFactory#initBeanWrapper

protected void initBeanWrapper(BeanWrapper bw) {
    bw.setConversionService(getConversionService());
    registerCustomEditors(bw);
}

ConversionService 是做屬性轉換的

registerCustomEditors 其實就是註冊一些屬性轉換器. 如:

org.springframework.beans.support.ResourceEditorRegistrar#registerCustomEditors

@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
    ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);
    doRegisterEditor(registry, Resource.class, baseEditor);
    doRegisterEditor(registry, ContextResource.class, baseEditor);
    doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
    doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
    doRegisterEditor(registry, File.class, new FileEditor(baseEditor));
    doRegisterEditor(registry, Path.class, new PathEditor(baseEditor));
    doRegisterEditor(registry, Reader.class, new ReaderEditor(baseEditor));
    doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));

    ClassLoader classLoader = this.resourceLoader.getClassLoader();
    doRegisterEditor(registry, URI.class, new URIEditor(classLoader));
    doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
    doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));

    if (this.resourceLoader instanceof ResourcePatternResolver) {
        doRegisterEditor(registry, Resource[].class,
                new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));
    }
}

 

autowireConstructor

想進這個方法, 其實也簡單. 方法有很多種, 其中一種最簡單的, 就是只放一個有參構造方法就行了.

在這裡, spring會根據自己的規則, 來進行 構造函數的選擇. 其選擇有限級為:

1. 優先按照訪問許可權, public 放在前面

2. 然後按照參數個數, 參數多的放在前面.

1. public IndexDao(Object o1, Object o2, Object o3)
2. public IndexDao(Object o1, Object o2)
3. public IndexDao(Object o1)
4. protected IndexDao(Integer i, Object o1, Object o2, Object o3)
5. protected IndexDao(Integer i, Object o1, Object o2)
6. protected IndexDao(Integer i, Object o1)

具體邏輯就不看了, 大致是這麼個效果.

當選擇好後, 就回進行創建工作. 當碰到構造參數時, 會去從容器中 獲取/創建 對象

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...