Spring Source - Create a singleton bean process

I. Introduction

For a good example of the singleton bean, getBean (String) method will not once again to create, but to get from the cache. If a bean has not been instantiated, this time can not hit the cache, this time, it is necessary to create the bean configuration information in accordance with the bean. Compared to implement the logic getBean (String) method to create bean method create (String, RootBeanDefinition, Object []) method and logic invoked more complicated, this will generally start with the analysis method createBean logic.

II. Source Analysis

2.1 bean instance is created entry

We must first know what createBean method is called, as follows:

public T doGetBean(...) {
    // 省略不相关代码
    if (mbd.isSingleton()) {
        sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
            @Override
            public Object getObject() throws BeansException {
                try {
                    return createBean(beanName, mbd, args);
                }
                catch (BeansException ex) {
                    destroySingleton(beanName);
                    throw ex;
                }
            }
        });
        bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
    }
    // 省略不相关代码
}

The above is a snippet doGetBean method, which can be found called createBean method. createBean method getObject method is wrapped anonymous factory class, but this was not an anonymous factory classes directly call getObject method, but passing itself as an argument to getSingleton (String, ObjectFactory) method. GetSingleton talked about before we realize this method, logic is as follows:

1. Get set start singletonObjects bean instance, if empty, is directly returned

2. If it is empty, enter the stage to create a bean instance. First beanName Add to singletonCurrentlyInCreation

3. Create a bean instance by calling method getObject method createBean

4. beanName removed from the collection singletonCurrentlyInCreation

5. <beanName, singletonObject> map cache set to singletonObjects

From the above analysis, to understand what may be called createBean time and place, down createBean do need to know what was going on.

2.2 createBean

Source code analysis as follows:

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

    // 解析 bean 的类型
    Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
    if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
        mbdToUse = new RootBeanDefinition(mbd);
        mbdToUse.setBeanClass(resolvedClass);
    }

    try {
        // 处理 lookup-method 和 replace-method 配置,Spring 将这两个配置统称为 override method
        mbdToUse.prepareMethodOverrides();
    }
    catch (BeanDefinitionValidationException ex) {
        throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
                beanName, "Validation of method overrides failed", ex);
    }

    try {
        // 在 bean 初始化前应用后置处理,如果后置处理返回的 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);
    }

    // 调用 doCreateBean 创建 bean
    Object beanInstance = doCreateBean(beanName, mbdToUse, args);
    if (logger.isDebugEnabled()) {
        logger.debug("Finished creating instance of bean '" + beanName + "'");
    }
    return beanInstance;
}

The implementation process is as follows:

1. The class attribute set or class according to resolve className

2. override attribute labeled and verified. Spring configuration is stored in the lookup-method and replace-method, which is to say two configurable load the configuration stored in the same BeanDefinition in methodOverrides property in, and the operation of this function is, in fact, for the two configurations .

3. initialized before the application processor, whether there is a short bean resolve the specified operation is initiated.

4. Create a bean.

2.3 Preparation and verification method override

Spring configuration is stored in the lookup-method and replace-method, which is to say two configurable load the configuration stored in the same BeanDefinition in methodOverrides property where these two functions to achieve the principle is in fact bean instantiation If it is detected methodOverrides properties, it dynamically generates a proxy for the current bean and using corresponding interceptors do bean enhancement processing, correlation logic implemented in the example of the bean will be mentioned.

public void prepareMethodOverrides() throws BeanDefinitionValidationException {
    MethodOverrides methodOverrides = getMethodOverrides();
    if (!methodOverrides.isEmpty()) {
        Set<MethodOverride> overrides = methodOverrides.getOverrides();
        synchronized (overrides) {
            // 循环处理每个 MethodOverride 对象
            for (MethodOverride mo : overrides) {
                prepareMethodOverride(mo);
            }
        }
    }
}

protected void prepareMethodOverride(MethodOverride mo) throws BeanDefinitionValidationException {
    // 获取方法名为 mo.getMethodName() 的方法数量,当方法重载时,count 的值就会大于1
    int count = ClassUtils.getMethodCountForName(getBeanClass(), mo.getMethodName());
    // count = 0,表明根据方法名未找到相应的方法,此时抛出异常
    if (count == 0) {
        throw new BeanDefinitionValidationException(
                "Invalid method override: no method with name '" + mo.getMethodName() +
                "' on class [" + getBeanClassName() + "]");
    }
    // 若 count = 1,表明仅存在已方法名为 mo.getMethodName(),这意味着方法不存在重载
    else if (count == 1) {
        // 方法不存在重载,则将 overloaded 成员变量设为 false
        mo.setOverloaded(false);
    }
}

The above source code, prepareMethodOverrides method called prepareMethodOverride cycle method, the other did too much logic. The main preparations are carried out in prepareMethodOverride method. prepareMethodOverride This method is mainly a method for acquiring the specified number of count method, and the corresponding processing according to the value of count. When count = 0, it indicates that the method does not exist, an exception is thrown. count = 1, setting MethodOverride object overloaded member variable to false. The aim is to advance tagging method name mo.getMethodName there is no overload, you do not need to be verified using enhanced CGLIB stage, find a direct method can be enhanced.

Examples of pre-processing of 2.4

BeanPostProcessor implement user interface and configured to implement class Spring configuration file (or use the annotations), custom actions can be carried out before and after the initialization bean.

protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
    Object bean = null;
    // 检测是否解析过,mbd.beforeInstantiationResolved 的值在下面的代码中会被设置
    if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
        if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
            Class<?> targetType = determineTargetType(beanName, mbd);
            if (targetType != null) {
                // 应用前置处理
                bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
                if (bean != null) {
                    // 应用后置处理
                    bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
                }
            }
        }
        // 设置 mbd.beforeInstantiationResolved
        mbd.beforeInstantiationResolved = (bean != null);
    }
    return bean;
}

After pre-processor application 2.4.1 instantiation

Instantiated before calling bean, that is, to convert AbstractBeanDefinition treatment before BeanWrapper. To subclass a chance to modify BeanDefinition, that is to say when the program after this method, we may not think of bean bean, but rather perhaps called after a bean processing agents, may also be generated by cglib may be through other technologies generated.

protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
    for (BeanPostProcessor bp : getBeanPostProcessors()) {
        // InstantiationAwareBeanPostProcessor 一般在 Spring 框架内部使用,不建议用户直接使用
        if (bp instanceof InstantiationAwareBeanPostProcessor) {
            InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
            // bean 初始化前置处理
            Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
            if (result != null) {
                return result;
            }
        }
    }
    return null;
}

2.4.2 Application of the post-processor instantiates

Spring is in the rules after bean initialization method postProcessAfterInitialization ensure as far as possible after registration of the application processor to the bean, because if the returned bean is not empty, then it will not go through the process of creating common bean again, so only here the application post-processor postProcessAfterInitialization.

public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
        throws BeansException {

    Object result = existingBean;
    for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
       // bean 初始化后置处理
        result = beanProcessor.postProcessAfterInitialization(result, beanName);
        if (result == null) {
            return result;
        }
    }
    return result;
}

2.5 calls doCreateBean method to create bean

After experiencing after resolveBeforeInstantiation method, the program has two options, if you create a proxy or rewritten postProcessBeforeInstantiation method InstantiationAwareBeanPostProcessor and change the bean in postBeforeInstantiation method, the direct return on the line, or you need to create a regular bean awakened, the conventional creation bean is doCreateBean of completion.

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

    /* 
     * BeanWrapper 是一个基础接口,由接口名可看出这个接口的实现类用于包裹 bean 实例。
     * 通过 BeanWrapper 的实现类可以方便的设置/获取 bean 实例的属性
     */
    BeanWrapper instanceWrapper = null;
    if (mbd.isSingleton()) {
        // 从缓存中获取 BeanWrapper,并清理相关记录
        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);
    }
    // 此处的 bean 可以认为是一个原始的 bean 实例,暂未填充属性
    final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
    Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
    mbd.resolvedTargetType = beanType;

    // 这里又遇到后置处理了,此处的后置处理是用于处理已“合并的 BeanDefinition”。
    synchronized (mbd.postProcessingLock) {
        if (!mbd.postProcessed) {
            try {
                applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
            }
            catch (Throwable ex) {
                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                        "Post-processing of merged bean definition failed", ex);
            }
            mbd.postProcessed = true;
        }
    }

    /*
     * earlySingletonExposure 是一个重要的变量,这里要说明一下。该变量用于表示是否提前暴露
     * 单例 bean,用于解决循环依赖。earlySingletonExposure 由三个条件综合而成,如下:
     *   条件1:mbd.isSingleton() - 表示 bean 是否是单例类型
     *   条件2:allowCircularReferences - 是否允许循环依赖
     *   条件3:isSingletonCurrentlyInCreation(beanName) - 当前 bean 是否处于创建的状态中
     * 
     * earlySingletonExposure = 条件1 && 条件2 && 条件3 
     *                        = 单例 && 是否允许循环依赖 && 是否存于创建状态中。
     */
    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");
        }
        // 添加工厂对象到 singletonFactories 缓存中
        addSingletonFactory(beanName, new ObjectFactory<Object>() {
            @Override
            public Object getObject() throws BeansException {
                // 获取早期 bean 的引用,如果 bean 中的方法被 AOP 切点所匹配到,此时 AOP 相关逻辑会介入
                return getEarlyBeanReference(beanName, mbd, bean);
            }
        });
    }

    Object exposedObject = bean;
    try {
        // 向 bean 实例中填充属性,populateBean 方法也是一个很重要的方法,后面会专门写文章分析
        populateBean(beanName, mbd, instanceWrapper);
        if (exposedObject != null) {
            /*
             * 进行余下的初始化工作,详细如下:
             * 1. 判断 bean 是否实现了 BeanNameAware、BeanFactoryAware、
             *    BeanClassLoaderAware 等接口,并执行接口方法
             * 2. 应用 bean 初始化前置操作
             * 3. 如果 bean 实现了 InitializingBean 接口,则执行 afterPropertiesSet 
             *    方法。如果用户配置了 init-method,则调用相关方法执行自定义初始化逻辑
             * 4. 应用 bean 初始化后置操作
             * 
             * 另外,AOP 相关逻辑也会在该方法中织入切面逻辑,此时的 exposedObject 就变成了
             * 一个代理对象了
             */
            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) {
            // 若 initializeBean 方法未改变 exposedObject 的引用,则此处的条件为 true。
            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.");
                }
            }
        }
    }

    try {
        // 注册销毁逻辑
        registerDisposableBeanIfNecessary(beanName, bean, mbd);
    }
    catch (BeanDefinitionValidationException ex) {
        throw new BeanCreationException(
                mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
    }

    return exposedObject;
}

Execution flow of the method is as follows:

1. If you need to clear the cache singleton

2. Examples of the bean, is converted to the BeanDefinition BeanWrapper. Conversion here are:

  • If there is a method using plant factory method to initialize
  • A class has multiple constructors, each constructor has different parameters, it is necessary to lock the constructor parameter and initialized
  • If neither the factory nor is there a constructor method with parameters, the default constructor for instantiating the bean

3. The application logic associated postprocessor MergedBeanDefinitionPostProcessor

4. The conditions determine whether the exposure time period or bean (early reference) in advance according to, for handling circular dependencies

The filling property, all the attributes of the instance of the bean filled

6. circular dependency checking, in this step will detect whether there has been loaded bean have been dependent on the cycle, and determine whether to throw an exception.

7. Registration DisposableBean, to call for the destruction of time

8. Create and complete return

 

Reference: the Spring container the IOC source analysis - the process of creating a singleton bean

           "Spring Source depth analysis"

Published 107 original articles · won praise 12 · views 20000 +

Guess you like

Origin blog.csdn.net/WYFVV/article/details/104555328