spring 5.0.21.BUILD-SNAPSHOT源码解读,第二集(Spring bean的生命周期)

在第一集的过程中已经将spring的源码编译并导入到idea中,那么下一步就开始创建自己的代码调试模块。

1.新建一个自己用户调试的moudle,并添加代码如下:

 

  

 具体代码已经放到码云上边了: https://gitee.com/rich_lz/spring_kz,所属分支: master 大家感兴趣的可以先下载,然后本地运行调试。

1. 根据AnnotationConfigApplicationContext 支持注解加载容器,与classpathxmlapplicationcontext 功能一样,只是支持后者采用配置方式,前者采用注解方式。

 

2.我们再来看初始化容器的参数

package com.audaque.learn.config;

import com.audaque.learn.process.MyBeanFactoryPostProcessor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;

/**
 * @ClassName AllConfig
 * @Descrription 统一信息配置类
 * @Author mxk
 * @Date 20-12-15 下午5:27
 * @Version 1.0
 */
//@Configuration
//@EnableAspectJAutoProxy
@ComponentScan(value = "com.audaque.learn")    //告诉spring从那个目录开始扫描,当扫描到有@Component,注解,就当作一个bean加载。
//@Import(value = MyBeanFactoryPostProcessor.class) //这个是bean加载完成之后的一个后置处理器,在这一讲中可以关闭掉。
public class AllConfig {


}

  

3. 我们再进入到AnnotationConfigApplicationContext 这个的构造方法中,我们发现有三个方法:

    public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
        /**
         * 2.初始化两个类(注册器)
         *         2.1 AnnotatedBeanDefinitionReader 主要用来注册带有注解的。
         *         2.2    ClassPathBeanDefinitionScanner 主要用于扫描符合条件的,并且带有注解的bean.
         */
        this();

        /**
         * 3.开始对第一步传进来的配置类做注入。
         *      就是将配置类解析成一个bean然后放入到bean的Map容器中。
         */
        register(annotatedClasses);


        /**
         * 重中之重的方法,
         */
        refresh();
    }

 

 

  第一个可以忽略不关注,主要就是初始化一个bean的解析器,一个扫描器:

  第二个方法稍微重要点,但也可以忽略,主要就是将我们自己定义的AllConfig,先注册成一个bean,因为其他的后续的很多操作都是需要依赖这个总配置的参数,所以需要优先加载,放置到beanDefinitionMap中(注意所有的bean的定义信息都是放在这个map中)。

  我们着重来第三个方法:

@Override
    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.

            /**
             *清空事件
             */
            prepareRefresh();
            /**
             *
             * 获取上一步生成的默认 DefaultListableBeanFactory 工厂(此类很很很重要)
             *
             */
            // Tell the subclass to refresh the internal bean factory.
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            /**
             * 设置beanFactory的很多很多参数,先不关注。
             */
            // Prepare the bean factory for use in this context.
            prepareBeanFactory(beanFactory);

            try {

                /**
                 * 什么也没做。
                 */
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);

                /**
                 * 实例化bean并调用beanfactory的后置处理器。
                 * 基本思路:
                 *     1.先扫描出那些bean实现了beanfactoryPostProceser接口,并且先初始化这些接口(getbean方法。)
                 *     2.开始调用初始化好的bean的postProcessBeanFactory方法,参数是已经全部注册好了的bean.这样我们就
                 *     对beanFacroty中所有的bean做任意修改,只要你愿意。
                 */
                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);

                /**
                 *注册bean的前置后置处理器,注意这里跟上边的区别,上边是针对bean定义做一个最后的修改的权限。
                 * 这里是对注册实现BeanPostProcessors接口的bean.
                 */
                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);

                /**
                 * 资源国际化的处理,先不考虑
                 */
                // Initialize message source for this context.
                initMessageSource();

                /**
                 * 初始化多播器。
                 */
                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();

                /**
                 * 啥也没做。
                 */
                // Initialize other special beans in specific context subclasses.
                onRefresh();

                // Check for listener beans and register them.
                registerListeners();

                /**
                 * 初始化其他的非懒加载到单例池。
                 */
                // Instantiate all remaining (non-lazy-init) singletons.
                finishBeanFactoryInitialization(beanFactory);

                // Last step: publish corresponding event.
                finishRefresh();
            }

            catch (BeansException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Exception encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
                }

                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();

                // Reset 'active' flag.
                cancelRefresh(ex);

                // Propagate exception to caller.
                throw ex;
            }

            finally {
                // Reset common introspection caches in Spring's core, since we
                // might not ever need metadata for singleton beans anymore...
                resetCommonCaches();
            }
        }
    }

 

5.我们再来诸个的分析我加粗的方法:

  invokeBeanFactoryPostProcessors:

   这个方法最终是委托给PostProcessorRegistrationDelegate代理类处理

        // Invoke BeanDefinitionRegistryPostProcessors first, if any.
        Set<String> processedBeans = new HashSet<>();

        if (beanFactory instanceof BeanDefinitionRegistry) {
            BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
            List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
            List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

            ........// Do not initialize FactoryBeans here: We need to leave all regular beans
            // uninitialized to let the bean factory post-processors apply to them!
            // Separate between BeanDefinitionRegistryPostProcessors that implement
            // PriorityOrdered, Ordered, and the rest.
            List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

            // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
            /**
             * 获取到的其实是 ConfigurationClassPostProcessor,因为ConfigurationClassPostProcessor实现BeanDefinitionRegistryPostProcess接口,并且是内置的bean(这里先不深究)。
             */
            String[] postProcessorNames =
                    beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                }
            }
            /**
             * 判断出ConfigurationClassPostProcesser接口,用于扫描所有的注解类。
             */
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            registryProcessors.addAll(currentRegistryProcessors);
            /**
             * 此处会根据配置类,扫描到所有符合条件的bean,会跳转到ConfigurationClassPostProcessor的postProcessBeanDefinitionRegistry方法。
        *该方法同样会继续调用processConfigBeanDefinitions方法。
*/ invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();

 

    

6.processConfigBeanDefinitions方法,这个方法中有两个最重要的方法,一个parse,一个loadbeanDefinition方法。  

public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {

List
<BeanDefinitionHolder> configCandidates = new ArrayList<>(); String[] candidateNames = registry.getBeanDefinitionNames(); //寻找到带有@Configuration的入口bean. //candidateNames 中保存的是目前容器中已经保存有的beans. for (String beanName : candidateNames) { //判断是否是Configuration配置类 BeanDefinition beanDef = registry.getBeanDefinition(beanName); if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) || ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) { if (logger.isDebugEnabled()) { logger.debug("Bean definition has already been processed as a configuration class: " + beanDef); } } else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) { configCandidates.add(new BeanDefinitionHolder(beanDef, beanName)); } } // Return immediately if no @Configuration classes were found if (configCandidates.isEmpty()) { return; } /** * 找到我们自定义的配置bean,也就是AllConfig */ // Sort by previously determined @Order value, if applicable configCandidates.sort((bd1, bd2) -> { int i1 = ConfigurationClassUtils.getOrder(bd1.getBeanDefinition()); int i2 = ConfigurationClassUtils.getOrder(bd2.getBeanDefinition()); return Integer.compare(i1, i2); }); ....../** * 获取到Allconfig配置类,开始扫描。 */ // Parse each @Configuration class ConfigurationClassParser parser = new ConfigurationClassParser( this.metadataReaderFactory, this.problemReporter, this.environment, this.resourceLoader, this.componentScanBeanNameGenerator, registry); Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates); Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size()); do { /** * 核心方法,根据ComponentScan属性值扫描。 * 该方法只是做了一个扫描bean,生成bean,装载bean的过程,并不实例化. */ parser.parse(candidates); parser.validate(); Set<ConfigurationClass> configClasses = new LinkedHashSet<>(parser.getConfigurationClasses()); configClasses.removeAll(alreadyParsed); /** * 注意此处才是开始载入bean,但是这里目前只是装载import引入的class。 */ this.reader.loadBeanDefinitions(configClasses); alreadyParsed.addAll(configClasses); candidates.clear(); if (registry.getBeanDefinitionCount() > candidateNames.length) { String[] newCandidateNames = registry.getBeanDefinitionNames(); Set<String> oldCandidateNames = new HashSet<>(Arrays.asList(candidateNames)); Set<String> alreadyParsedClasses = new HashSet<>(); for (ConfigurationClass configurationClass : alreadyParsed) { alreadyParsedClasses.add(configurationClass.getMetadata().getClassName()); } for (String candidateName : newCandidateNames) { if (!oldCandidateNames.contains(candidateName)) { BeanDefinition bd = registry.getBeanDefinition(candidateName); if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) && !alreadyParsedClasses.contains(bd.getBeanClassName())) { candidates.add(new BeanDefinitionHolder(bd, candidateName)); } } } candidateNames = newCandidateNames; } } }

 

 7.我们再接着看  parser.parse(candidates);方法,这个方法先获取到用户自定义的配置Allconfig,并解析出@ComponentScan这个参数值,并组装成componentScans,扫描所有的class封装成bean,一并放入到beandifinitionMap中。

 

8.parse的核心方法在: doProcessConfigurationClass。

if (existingClass != null) {
// Recursively process the configuration class and its superclass hierarchy.
        SourceClass sourceClass = asSourceClass(configClass);
        do {//核心,扫描出所有符合条件的bean.
            sourceClass = doProcessConfigurationClass(configClass, sourceClass);
        }
        while (sourceClass != null);

        this.configurationClasses.put(configClass, configClass);
    }

 

 

9.我们再次回到第二步骤中的registerBeanPostProcessors 方法,这个方法主要是获取到上边几步中注册的bean,然后找到BeanPostProcess接口的bean,将这些bean放入到beanfactory中。

10.finishBeanFactoryInitialization开始实例化非懒加载的bean:

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
  //省略代码。
/** * 这里真正的实例化所有的bean. */ // Instantiate all remaining (non-lazy-init) singletons. beanFactory.preInstantiateSingletons(); }

 

 11.再次进入到preInstantiateSingletons方法:

        if (logger.isDebugEnabled()) {
            logger.debug("Pre-instantiating singletons in " + this);
        }

        // Iterate over a copy to allow for init methods which in turn register new bean definitions.
        // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
        List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

        // 前边已经全部将bean通过parse方法解析完成,接下来就是实例化了。
for (String beanName : beanNames) { RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { if (isFactoryBean(beanName)) { Object bean = getBean(FACTORY_BEAN_PREFIX + beanName); if (bean instanceof FactoryBean) { FactoryBean<?> factory = (FactoryBean<?>) bean; boolean isEagerInit; if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { isEagerInit = AccessController.doPrivileged( (PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit, getAccessControlContext()); } else { isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factory).isEagerInit()); } if (isEagerInit) { getBean(beanName); } } } else {
            //核心方法。 getBean(beanName); } } }
// Trigger post-initialization callback for all applicable beans... for (String beanName : beanNames) { Object singletonInstance = getSingleton(beanName); if (singletonInstance instanceof SmartInitializingSingleton) { SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance; if (System.getSecurityManager() != null) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { smartSingleton.afterSingletonsInstantiated(); return null; }, getAccessControlContext()); } else { smartSingleton.afterSingletonsInstantiated(); } }
}

 12 我们再次进入到getBean(beanName);方法,最终市会调用AbstractBeanFactory的doGetBean方法,

@SuppressWarnings("unchecked")
    protected <T> T doGetBean(
            String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
            throws BeansException {
    //转换bean名字。
        String beanName = transformedBeanName(name);
        Object bean;

        // 先看看单例池有没有这个bean.
        Object sharedInstance = getSingleton(beanName);
        if (sharedInstance != null && args == null) {
            if (logger.isDebugEnabled()) {
                if (isSingletonCurrentlyInCreation(beanName)) {
                    logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
                            "' that is not fully initialized yet - a consequence of a circular reference");
                }
                else {
                    logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
                }
            }
            bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
        }

        else {
            /**
             * 第一次从单例池获取对象为空。
             */
            // Fail if we're already creating this bean instance:
            // We're assumably within a circular reference.
            if (isPrototypeCurrentlyInCreation(beanName)) {
                throw new BeanCurrentlyInCreationException(beanName);
            }

            // 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 (parentBeanFactory instanceof AbstractBeanFactory) {
                    return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
                            nameToLookup, requiredType, args, typeCheckOnly);
                }
                else 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) {
                //标记当前bean正在创建。
                markBeanAsCreated(beanName);
            }

            try {
                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 dep : dependsOn) {
                        if (isDependent(beanName, dep)) {
                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
                        }
                        registerDependentBean(dep, beanName);
                        try {
                            getBean(dep);
                        }
                        catch (NoSuchBeanDefinitionException ex) {
                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "'" + beanName + "' depends on missing bean '" + dep + "'", ex);
                        }
                    }
                }
                /**
                 * 开始实例化bean.
                 */
                // Create bean instance.
                if (mbd.isSingleton()) {
                    sharedInstance = getSingleton(beanName, () -> {
                        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); } } // Check if required type matches the type of the actual bean instance. if (requiredType != null && !requiredType.isInstance(bean)) { try { T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType); if (convertedBean == null) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return convertedBean; } 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; }

13那么我们接着跟到createBean中,这里会调用到AbstractAutowireCapableBeanFactory的doCreateBean方法:

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

        // Instantiate the bean.
        BeanWrapper instanceWrapper = null;
        if (mbd.isSingleton()) {
            instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
        }
        if (instanceWrapper == null) {
            //第1步,实例化对象,根据构造函数实例化对象。
            instanceWrapper = createBeanInstance(beanName, mbd, args);
        }
        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 {
                    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.isDebugEnabled()) {
                logger.debug("Eagerly caching bean '" + beanName +
                        "' to allow for resolving potential circular references");
            }
            /**
             * 将已经实例化的对象放入到singletonFactories,registeredSingletons 中。
             */
            addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
        }

        // Initialize the bean instance.
        Object exposedObject = bean;
        try {
            //第二步,填充属性,解决依赖问题。
            populateBean(beanName, mbd, instanceWrapper);
            //调用initmethod方法,初始化。
            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 " +
                                "'getBeanNamesForType' 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;
    }

14.我们在第13步最后返回的exposedObject 就是最终实例化的一个bean,它会先把这个组装好的bean放入单例池,然后返回调用方,在第12步的时候再根据这个13步返回的sharedInstance去单例池拿这个对象,最后逐级返回。

15.总结:

  1.  先实例化配置bean,就是工程中AllConfig.java.

  2.  准备beanfactory,bean解析器,扫描器。
  3. 调用beanFactoryPostProcess,这个前提是先初始化实现beanFactoryPostProcess接口的类。然后调用parse方法,最后使用我们的beanFactoryPostProcess的实现类去调整spring的bean定义,这个也是spring对外的一个允许用户修改bean定义的口子。
  4. 开始注册beanPostProcess的接口实现类。
  5. 资源国际化。
  6. 多播器。
  7. 开始初始化bean对象。

16结语:  其实源码根本没那么可怕,只需要带着问题一级一级往下跟,另外分享个调试小技巧: 我调试代码喜欢先看结果,如果不确定有些代码的功能逻辑,可以在idea中eEaluateExpression中先执行查看结果,2.在断点中使用条件判断。

    

 

posted on 2020-12-29 16:13  康仔☆  阅读(211)  评论(0)    收藏  举报

导航