Spring 启动流程概述

Spring 启动流程概述

对于 Spring 启动流程和 Bean 的生命周期,总有一些小地方搞的不是很清楚,干脆直接通过修改代码增加日志输出,使用断点单步调试,把整个流程捋顺了一点点的。

除了加载配置文件或者基础配置类外,Spring 的启动过程几乎都被封装在 AbstractApplicationContext#refresh 方法中,可以说弄清楚了这个方法的执行过程,就摸清楚了 Spring 启动全流程,下面的流程分析也是以这个方法为骨架来展开的。

流程概要

下面完整流程有些太复杂,所以,提炼一个简要的过程,方便糊弄面试官,哈哈哈😆

  1. 创建容器,读取 applicationContext.register(Config.class) 指定的配置。

  2. 准备 BeanFactory,注册容器本身和 BeanFactory 实例,以及注册环境配置信息等。

  3. 执行 BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry 注册 BeanDefinition。有三点需要注意:

    1. 目前只有一个 ConfigurationClassPostProcessor 实现类,Spring 中大量的 Bean 都是在这一步被该类注册到容器中的。

    2. 执行顺序是 ① PriorityOrderedOrdered ③ 普通的顺序来执行

    3. 在执行上一步是,如果发现注册了 BeanDefinitionRegistryPostProcessor 类型的 Bean,就会在循环里继续调用 postProcessBeanDefinitionRegistry 方法。MyBATIS 和 Spring 整合的 MapperScannerConfigurer 类就是在这一步执行的。

  4. 执行 BeanFactoryPostProcessor#postProcessBeanFactory 方法。目前只有一个 ConfigurationClassPostProcessor 实现类。

  5. 注册 CommonAnnotationBeanPostProcessorAutowiredAnnotationBeanPostProcessorBeanPostProcessor

  6. 注册 ApplicationEventMulticaster,用于广播事件的。

  7. 注册 ApplicationListener

  8. 预加载以及注册所有非懒加载的 Bean

完整启动流程

  1. 调用 prepareRefresh() 方法,初始化属性源(property source)配置。

  2. 调用 obtainFreshBeanFactory() 获得 ConfigurableListableBeanFactory 对象。

  3. 调用 prepareBeanFactory,准备 BeanFactory,添加必要的 Bean;添加 ApplicationContextAwareProcessorApplicationListenerDetector 处理器;注册环境相关的 Bean。

  4. 下面通过 AbstractApplicationContext#invokeBeanFactoryPostProcessors 方法,开始执行 BeanDefinitionRegistryPostProcessorBeanFactoryPostProcessor 相关的方法。这个方法流程起始也很简单:

    目前,除了用户自定义的 BeanDefinitionRegistryPostProcessorBeanFactoryPostProcessor 外,Spring 内置的,只有 ConfigurationClassPostProcessor 一个类。所以,把这个类的实现摸清楚了,AbstractApplicationContext#invokeBeanFactoryPostProcessors 就可以跳过了。

    1. 首先,执行 BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry 方法,顺序如下:

      1. 用户手动添加的 BeanDefinitionRegistryPostProcessor

      2. 实现 PriorityOrdered 接口的 BeanDefinitionRegistryPostProcessor

      3. 实现 Ordered 接口的 BeanDefinitionRegistryPostProcessor

      4. 普通 BeanDefinitionRegistryPostProcessor,只要发现有新加入的,就循环调用。

    2. 然后,执行 BeanFactoryPostProcessor#postProcessBeanFactory 方法。顺序如下:

      1. 实现 BeanDefinitionRegistryPostProcessor 接口的类;

      2. 实现 BeanFactoryPostProcessor 接口的类。

  5. 先执行用户手动添加的 BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry(DefaultListableBeanFactory)

    关于 BeanDefinitionRegistryPostProcessor 的处理流程,D瓜哥在 Spring 扩展点概览及实践:BeanDefinitionRegistryPostProcessor 中有更详细的描述,不了解的朋友请参考那篇文章的介绍。

  6. 创建 ConfigurationClassPostProcessor 对象,并针对该对象依次执行

    1. 构造函数

    2. ApplicationListenerDetector#postProcessMergedBeanDefinition(RootBeanDefinition, ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor)

    3. 调用用户手动添加的 BeanPostProcessor#postProcessBeforeInitialization 方法

    4. ApplicationContextAwareProcessor#postProcessBeforeInitialization(ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor)

    5. ApplicationListenerDetector#postProcessBeforeInitialization(ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor)

    6. 执行 init 方法

    7. 调用用户手动添加的 BeanPostProcessor#postProcessAfterInitialization 方法

    8. ApplicationContextAwareProcessor#postProcessAfterInitialization(ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor) — 由于 ApplicationContextAwareProcessor 并没有该方法,所以不执行。

    9. ApplicationListenerDetector#postProcessBeforeInitialization(ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor)

  7. 执行 ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry(DefaultListableBeanFactory) — 在这里,处理 @Configuration@Import@ImportResource@Bean 和 。

  8. 执行用户手动添加的 BeanDefinitionRegistryPostProcessor#postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)

  9. 执行 ConfigurationClassPostProcessor#postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) — 在这里给 @Configuration 标注的类,生成 cglib 增强后的代理类。注意:在这里,还增加了一个 ImportAwareBeanPostProcessor 后置处理器。

    因为 ConfigurationClassPostProcessor 是一个 InstantiationAwareBeanPostProcessor 实例。所以,实例化 ConfigurationClassPostProcessor 对象并加入到容器后。这句话啥意思?想想再补充一下。

  10. 创建了 EventListenerMethodProcessor 实例,和创建 ConfigurationClassPostProcessor 时类似,依次执行

    1. InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation — 目前有 ImportAwareBeanPostProcessor

    2. 构造函数

    3. MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition — 目前有 ApplicationListenerDetector

    4. InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation

    5. InstantiationAwareBeanPostProcessor#postProcessProperties — 目前有 ImportAwareBeanPostProcessor

    6. InstantiationAwareBeanPostProcessor#postProcessPropertyValues — 从 5.1 开始废弃,使用上面方法代替。

    7. BeanPostProcessor#postProcessBeforeInitialization — 目前有

      1. 用户手动添加的 BeanPostProcessor

      2. ApplicationContextAwareProcessor

      3. ApplicationListenerDetector

      4. ImportAwareBeanPostProcessor

    8. init

    9. BeanPostProcessor#postProcessAfterInitialization 方法。 — 与 postProcessBeforeInitialization 相同,不再赘述。

      有一点需要注意,上面增加了 ImportAwareBeanPostProcessor 实例,这里也会执行。以下都是如此,不再赘述。

  11. 实例化用户通过 BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry(DefaultListableBeanFactory) 或者 @Configuration 添加的 BeanFactoryPostProcessor,以及 Spring 自己添加的 BeanFactoryPostProcessor。依次执行如下方法:

    1. InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation — 目前有 ImportAwareBeanPostProcessor

    2. Bean 的构造函数

    3. MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition — 目前有 ApplicationListenerDetector

    4. InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation

    5. InstantiationAwareBeanPostProcessor#postProcessProperties — 目前有 ImportAwareBeanPostProcessor

    6. InstantiationAwareBeanPostProcessor#postProcessPropertyValues — 从 5.1 开始废弃,使用上面方法代替。

    7. BeanPostProcessor#postProcessBeforeInitialization — 目前有

      1. 用户手动添加的 BeanPostProcessor

      2. ApplicationContextAwareProcessor

      3. ApplicationListenerDetector

      4. ImportAwareBeanPostProcessor

    8. init

    9. BeanPostProcessor#postProcessAfterInitialization 方法

  12. 调用上一步创建的 BeanFactoryPostProcessor 对象的 postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) 方法。这里目前包含 EventListenerMethodProcessor 对象。EventListenerMethodProcessorAnnotationConfigApplicationContext() 初始化时,创建 new AnnotatedBeanDefinitionReader(this) 对象时,通过调用 AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry) 方法注册到容器中的。

    1. 这里调用 EventListenerMethodProcessor#postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory),创建 EventListenerFactory 对象,依次执行

      这个 EventListenerFactory 对象不重要。或者说,目前没有发现它特别重要的地方。

      1. InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation

      2. Bean 的构造函数

      3. MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition

      4. InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation

      5. InstantiationAwareBeanPostProcessor#postProcessProperties

      6. InstantiationAwareBeanPostProcessor#postProcessPropertyValues — 从 5.1 开始废弃,使用上面方法代替。

      7. BeanPostProcessor#postProcessBeforeInitialization

      8. init

      9. BeanPostProcessor#postProcessAfterInitialization 方法

  13. 到此为止,invokeBeanFactoryPostProcessors(beanFactory) 方法调用完毕。

  14. 下面开始调用 registerBeanPostProcessors(beanFactory) 方法。

  15. 添加 PostProcessorRegistrationDelegate.BeanPostProcessorChecker 实例,以下执行 BeanPostProcessor 方法时,都会带上。

  16. 创建 AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor 对象,依次执行如下方法:

    1. InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation — 目前有 ImportAwareBeanPostProcessor

    2. 构造函数

    3. MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition — 目前有 ApplicationListenerDetector

    4. InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation

    5. InstantiationAwareBeanPostProcessor#postProcessProperties

    6. InstantiationAwareBeanPostProcessor#postProcessPropertyValues — 从 5.1 开始废弃,使用上面方法代替。

    7. AutowiredAnnotationBeanPostProcessor#setBeanFactory(DefaultListableBeanFactory) — 完成 BeanNameAwareBeanClassLoaderAwareBeanFactoryAware 三个 Aware 的注入。通过 AbstractAutowireCapableBeanFactory#invokeAwareMethods 方法来完成。

    8. BeanPostProcessor#postProcessBeforeInitialization — 目前有

      1. 用户手动添加的 BeanPostProcessor

      2. ApplicationContextAwareProcessor — 完成如下六个 Aware 的注入:

        1. EnvironmentAware

        2. EmbeddedValueResolverAware

        3. ResourceLoaderAware

        4. ApplicationEventPublisherAware

        5. MessageSourceAware

        6. ApplicationContextAware

      3. ApplicationListenerDetector

      4. ImportAwareBeanPostProcessor

      5. BeanPostProcessorChecker

    9. init

    10. BeanPostProcessor#postProcessAfterInitialization 方法

  17. AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor 对象注册到容器中。以下会随着 BeanPostProcessor 的调用,也会被执行。

  18. 创建 AnnotationAwareAspectJAutoProxyCreator 对象,依次执行如下方法:

    1. InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation — 目前有如下三个:

      1. ImportAwareBeanPostProcessor

      2. CommonAnnotationBeanPostProcessor

      3. AutowiredAnnotationBeanPostProcessor

    2. 构造函数

    3. MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition — 目前有如下三个:

      1. ApplicationListenerDetector

      2. CommonAnnotationBeanPostProcessor — 收集依赖信息。

      3. AutowiredAnnotationBeanPostProcessor — 收集依赖信息。

    4. InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation

    5. InstantiationAwareBeanPostProcessor#postProcessProperties 目前有如下三个:

      1. ImportAwareBeanPostProcessor

      2. CommonAnnotationBeanPostProcessor — 完成依赖注入。

      3. AutowiredAnnotationBeanPostProcessor — 完成依赖注入。

    6. InstantiationAwareBeanPostProcessor#postProcessPropertyValues — 从 5.1 开始废弃,使用上面方法代替。

    7. BeanPostProcessor#postProcessBeforeInitialization — 目前有

      1. 用户手动添加的 BeanPostProcessor

      2. ApplicationContextAwareProcessor — 完成如下六个 Aware 的注入:

        1. EnvironmentAware

        2. EmbeddedValueResolverAware

        3. ResourceLoaderAware

        4. ApplicationEventPublisherAware

        5. MessageSourceAware

        6. ApplicationContextAware

      3. ApplicationListenerDetector

      4. ImportAwareBeanPostProcessor

      5. BeanPostProcessorChecker

      6. CommonAnnotationBeanPostProcessor

      7. AutowiredAnnotationBeanPostProcessor

    8. init

    9. BeanPostProcessor#postProcessAfterInitialization 方法

  19. AnnotationAwareAspectJAutoProxyCreator 对象注册到容器中。以下会随着 BeanPostProcessor 的调用,也会被执行。

  20. 重新添加 ApplicationListenerDetector,其实就是换了个位置,将其调整到了最后。

  21. 到此为止,registerBeanPostProcessors(beanFactory) 方法调用完毕。

  22. 调用 initMessageSource() 方法,注册 MessageSource Bean。

  23. 调用 initApplicationEventMulticaster() 方法,注册 SimpleApplicationEventMulticaster 对象,

  24. 调用 onRefresh() 方法,这是空方法,方便做扩展。

  25. 调用 registerListeners() 方法,但是似乎什么也没做。

  26. 调用 finishBeanFactoryInitialization(beanFactory) 方法,这个方法中,最重要的一个操作就是实例化非懒加载的所有 Bean,在 DefaultListableBeanFactory#preInstantiateSingletons 中完成这些操作。目前,除了用户自己实现的,还有七个如下的 BeanPostProcessor

    1. ApplicationContextAwareProcessor

    2. ConfigurationClassPostProcessor

    3. BeanPostProcessorChecker

    4. AnnotationAwareAspectJAutoProxyCreator

    5. CommonAnnotationBeanPostProcessor

    6. AutowiredAnnotationBeanPostProcessor

    7. ApplicationListenerDetector

      这部分内容放在下一篇文章 Spring Bean 生命周期概述 再展开来讲。

  27. 调用 finishRefresh() — 启动生命周期函数,广播刷新完成通知。具体如下:

    1. 清理 Resource 缓存(也就是被扫描到的各种类,自定义类,以及相关父类和所实现的接口)。(像是在 ImportSelector 中声明的类。但是没有找到添加到缓存的地方?)

    2. 注册 LifecycleProcessor,并通过它启动所有的 LifecycleProcessor 和它自身。没有看出来干什么用的?

    3. 广播 ContextRefreshedEvent 事件。

    4. ConfigurableApplicationContext 注册到 LiveBeansView 上,如果它存在的话。

    5. 清理各种缓存

      1. 启动过程中的反射相关缓存,比如 init-methodAware 相关的方法,注入需要的字段等等;

      2. AnnotationFilter 相关缓存;

      3. 注解元素缓存和生命周期函数(AwareInitializingBean、`BeanFactoryPostProcessor`等)缓存清空

      4. 解析类型缓存清空

      5. 反省结果清空

在下一篇文章 Spring Bean 生命周期概述 中,D瓜哥将针对 Spring Bean 的整个生命周期展开详细说明。

附录:启动日志

下面是启动日志。有删减,为了方便阅读,增加了序号和层次。

  1. 调用 prepareRefresh() 方法,初始化属性源(property source)配置。

  2. 调用 obtainFreshBeanFactory() 获得 ConfigurableListableBeanFactory 对象。

  3. 准备 BeanFactory,添加必要的 Bean,在 prepareBeanFactory 中完成。

  4. 下面通过 invokeBeanFactoryPostProcessors 方法,开始执行 BeanFactoryPostProcessor 相关的方法

  5. LogBeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry(DefaultListableBeanFactory) — 用户自己手动添加的 BeanDefinitionRegistryPostProcessor 实例

  6. 创建 ConfigurationClassPostProcessor Bean

    1. 构造函数

    2. ApplicationListenerDetector#postProcessMergedBeanDefinition(RootBeanDefinition, ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor) — ApplicationListenerDetector 实例是在 prepareBeanFactory 方法中,加入到容器中的。

    3. LogBeanPostProcessor#postProcessBeforeInitialization(ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor) — 用户自己手动添加

    4. LogDestructionAwareBeanPostProcessor#postProcessBeforeInitialization(ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor) — 用户自己手动添加,继承默认实现。

    5. ApplicationContextAwareProcessor#postProcessBeforeInitialization(ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor) — ApplicationContextAwareProcessor 实例是在 prepareBeanFactory 方法中,加入到容器中的。处理六种 Aware 注入。

    6. ApplicationListenerDetector#postProcessBeforeInitialization(ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor)

    7. LogBeanPostProcessor#postProcessAfterInitialization(ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor)

    8. LogDestructionAwareBeanPostProcessor#postProcessAfterInitialization(ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor) — 用户自己手动添加,继承默认实现,没有任何操作。

    9. ApplicationContextAwareProcessor#postProcessAfterInitialization(ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor) — 继承默认实现,没有任何操作。

    10. ApplicationListenerDetector#postProcessAfterInitialization(ConfigurationClassPostProcessor, org.springframework.context.annotation.internalConfigurationAnnotationProcessor)

  7. ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry(DefaultListableBeanFactory) — 在这里,处理 @Configuration@Import@ImportResource@Bean 和 。

  8. LogBeanDefinitionRegistryPostProcessor#postProcessBeanFactory(DefaultListableBeanFactory)

  9. ConfigurationClassPostProcessor#postProcessBeanFactory(DefaultListableBeanFactory) — 在这里给 @Configuration 标注的类,生成 cglib 增强后的代理类。注意:在这里,还增加了一个 ImportAwareBeanPostProcessor 后置处理器。

    因为 ConfigurationClassPostProcessor 是一个 InstantiationAwareBeanPostProcessor 实例。所以,实例化 ConfigurationClassPostProcessor 对象并加入到容器后。这句话啥意思?想想再补充一下。

  10. 创建 EventListenerMethodProcessor Bean, Name: org.springframework.context.event.internalEventListenerProcessor

    1. ImportAwareBeanPostProcessor#postProcessBeforeInstantiation(EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    2. 构造函数

    3. ApplicationListenerDetector#postProcessMergedBeanDefinition(RootBeanDefinition, EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    4. ImportAwareBeanPostProcessor#postProcessAfterInstantiation(EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    5. ImportAwareBeanPostProcessor#postProcessProperties(MutablePropertyValues, EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    6. LogBeanPostProcessor#postProcessBeforeInitialization(EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    7. LogDestructionAwareBeanPostProcessor#postProcessBeforeInitialization(EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    8. ApplicationContextAwareProcessor#postProcessBeforeInitialization(EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    9. ApplicationListenerDetector#postProcessBeforeInitialization(EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    10. ImportAwareBeanPostProcessor#postProcessBeforeInitialization(EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    11. LogBeanPostProcessor#postProcessAfterInitialization(EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    12. LogDestructionAwareBeanPostProcessor#postProcessAfterInitialization(EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    13. ApplicationContextAwareProcessor#postProcessAfterInitialization(EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    14. ApplicationListenerDetector#postProcessAfterInitialization(EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

    15. ImportAwareBeanPostProcessor#postProcessAfterInitialization(EventListenerMethodProcessor, org.springframework.context.event.internalEventListenerProcessor)

  11. 创建自定义 LogBeanFactoryPostProcessor,通过上面 LogBeanDefinitionRegistryPostProcessorpostProcessBeanDefinitionRegistry 方法添加。在这一步创建用户通过 BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry(DefaultListableBeanFactory) 或者 @Configuration 添加的 BeanFactoryPostProcessor,以及 Spring 自己添加的 BeanFactoryPostProcessor 等类的相关 Bean。

    1. ImportAwareBeanPostProcessor#postProcessBeforeInstantiation(LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    2. ApplicationListenerDetector#postProcessMergedBeanDefinition(RootBeanDefinition, LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    3. ImportAwareBeanPostProcessor#postProcessAfterInstantiation(LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    4. ImportAwareBeanPostProcessor#postProcessProperties(MutablePropertyValues, LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    5. LogBeanPostProcessor#postProcessBeforeInitialization(LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    6. LogDestructionAwareBeanPostProcessor#postProcessBeforeInitialization(LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    7. ApplicationContextAwareProcessor#postProcessBeforeInitialization(LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    8. ApplicationListenerDetector#postProcessBeforeInitialization(LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    9. ImportAwareBeanPostProcessor#postProcessBeforeInitialization(LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    10. LogBeanPostProcessor#postProcessAfterInitialization(LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    11. LogDestructionAwareBeanPostProcessor#postProcessAfterInitialization(LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    12. ApplicationContextAwareProcessor#postProcessAfterInitialization(LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    13. ApplicationListenerDetector#postProcessAfterInitialization(LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

    14. ImportAwareBeanPostProcessor#postProcessAfterInitialization(LogBeanFactoryPostProcessor, LogBeanFactoryPostProcessor)

  12. 这里会调用上一步创建的 BeanFactoryPostProcessor 对象的 postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) 方法。这里目前包含 EventListenerMethodProcessor 对象。EventListenerMethodProcessorAnnotationConfigApplicationContext() 初始化时,创建 new AnnotatedBeanDefinitionReader(this) 对象时,通过调用 AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry) 方法注册到容器中的。

  13. LogBeanFactoryPostProcessor#postProcessBeanFactory(DefaultListableBeanFactory)

  14. 到此为止,invokeBeanFactoryPostProcessors(beanFactory) 方法调用完毕。

  15. 下面开始调用 registerBeanPostProcessors(beanFactory) 方法。

  16. 添加 PostProcessorRegistrationDelegate.BeanPostProcessorChecker 实例,以下执行 BeanPostProcessor 方法时,都会带上。

  17. 创建 AutowiredAnnotationBeanPostProcessor Bean,Name: org.springframework.context.annotation.internalAutowiredAnnotationProcessor

    1. ImportAwareBeanPostProcessor#postProcessBeforeInstantiation(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    2. ApplicationListenerDetector#postProcessMergedBeanDefinition(RootBeanDefinition, AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    3. ImportAwareBeanPostProcessor#postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    4. ImportAwareBeanPostProcessor#postProcessProperties(MutablePropertyValues, AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    5. AutowiredAnnotationBeanPostProcessor#setBeanFactory(DefaultListableBeanFactory)

    6. LogBeanPostProcessor#postProcessBeforeInitialization(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    7. LogDestructionAwareBeanPostProcessor#postProcessBeforeInitialization(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    8. ApplicationContextAwareProcessor#postProcessBeforeInitialization(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    9. ApplicationListenerDetector#postProcessBeforeInitialization(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    10. ImportAwareBeanPostProcessor#postProcessBeforeInitialization(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    11. BeanPostProcessorChecker#postProcessBeforeInitialization(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    12. LogBeanPostProcessor#postProcessAfterInitialization(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    13. LogDestructionAwareBeanPostProcessor#postProcessAfterInitialization(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    14. ApplicationContextAwareProcessor#postProcessAfterInitialization(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    15. ApplicationListenerDetector#postProcessAfterInitialization(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    16. ImportAwareBeanPostProcessor#postProcessAfterInitialization(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

    17. BeanPostProcessorChecker#postProcessAfterInitialization(AutowiredAnnotationBeanPostProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor)

  18. 创建 CommonAnnotationBeanPostProcessor Bean,Name: org.springframework.context.annotation.internalCommonAnnotationProcessor

    1. ImportAwareBeanPostProcessor#postProcessBeforeInstantiation(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    2. ApplicationListenerDetector#postProcessMergedBeanDefinition(RootBeanDefinition, CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    3. ImportAwareBeanPostProcessor#postProcessAfterInstantiation(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    4. ImportAwareBeanPostProcessor#postProcessProperties(MutablePropertyValues, CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    5. LogBeanPostProcessor#postProcessBeforeInitialization(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    6. LogDestructionAwareBeanPostProcessor#postProcessBeforeInitialization(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    7. ApplicationContextAwareProcessor#postProcessBeforeInitialization(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    8. ApplicationListenerDetector#postProcessBeforeInitialization(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    9. ImportAwareBeanPostProcessor#postProcessBeforeInitialization(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    10. BeanPostProcessorChecker#postProcessBeforeInitialization(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    11. LogBeanPostProcessor#postProcessAfterInitialization(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    12. LogDestructionAwareBeanPostProcessor#postProcessAfterInitialization(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    13. ApplicationContextAwareProcessor#postProcessAfterInitialization(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    14. ApplicationListenerDetector#postProcessAfterInitialization(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    15. ImportAwareBeanPostProcessor#postProcessAfterInitialization(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

    16. BeanPostProcessorChecker#postProcessAfterInitialization(CommonAnnotationBeanPostProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor)

  19. 创建 AnnotationAwareAspectJAutoProxyCreator,Name: org.springframework.aop.config.internalAutoProxyCreator。也许是因为配置了 @EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)这个再探究竟?

    1. ImportAwareBeanPostProcessor#postProcessBeforeInstantiation(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    2. CommonAnnotationBeanPostProcessor#postProcessBeforeInstantiation(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    3. AutowiredAnnotationBeanPostProcessor#postProcessBeforeInstantiation(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    4. ApplicationListenerDetector#postProcessMergedBeanDefinition(RootBeanDefinition, AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    5. CommonAnnotationBeanPostProcessor#postProcessMergedBeanDefinition(RootBeanDefinition, AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    6. AutowiredAnnotationBeanPostProcessor#postProcessMergedBeanDefinition(RootBeanDefinition, AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    7. ImportAwareBeanPostProcessor#postProcessAfterInstantiation(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    8. CommonAnnotationBeanPostProcessor#postProcessAfterInstantiation(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    9. AutowiredAnnotationBeanPostProcessor#postProcessAfterInstantiation(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    10. ImportAwareBeanPostProcessor#postProcessProperties(MutablePropertyValues, AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    11. CommonAnnotationBeanPostProcessor#postProcessProperties(MutablePropertyValues, AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    12. AutowiredAnnotationBeanPostProcessor#postProcessProperties(MutablePropertyValues, AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    13. LogBeanPostProcessor#postProcessBeforeInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    14. LogDestructionAwareBeanPostProcessor#postProcessBeforeInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    15. ApplicationContextAwareProcessor#postProcessBeforeInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    16. ApplicationListenerDetector#postProcessBeforeInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    17. ImportAwareBeanPostProcessor#postProcessBeforeInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    18. BeanPostProcessorChecker#postProcessBeforeInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    19. CommonAnnotationBeanPostProcessor#postProcessBeforeInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    20. AutowiredAnnotationBeanPostProcessor#postProcessBeforeInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    21. LogBeanPostProcessor#postProcessAfterInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    22. LogDestructionAwareBeanPostProcessor#postProcessAfterInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    23. ApplicationContextAwareProcessor#postProcessAfterInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    24. ApplicationListenerDetector#postProcessAfterInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    25. ImportAwareBeanPostProcessor#postProcessAfterInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    26. BeanPostProcessorChecker#postProcessAfterInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    27. CommonAnnotationBeanPostProcessor#postProcessAfterInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

    28. AutowiredAnnotationBeanPostProcessor#postProcessAfterInitialization(AnnotationAwareAspectJAutoProxyCreator, org.springframework.aop.config.internalAutoProxyCreator)

  20. 预加载 ConfigUserService 等 Bean。下面以 UserService 为例:

    1. ImportAwareBeanPostProcessor#postProcessBeforeInstantiation(UserService, UserService)

    2. AnnotationAwareAspectJAutoProxyCreator#postProcessBeforeInstantiation(UserService, UserService)

    3. CommonAnnotationBeanPostProcessor#postProcessBeforeInstantiation(UserService, UserService)

    4. AutowiredAnnotationBeanPostProcessor#postProcessBeforeInstantiation(UserService, UserService)

    5. 构造函数

    6. CommonAnnotationBeanPostProcessor#postProcessMergedBeanDefinition(RootBeanDefinition, UserService, UserService)

    7. AutowiredAnnotationBeanPostProcessor#postProcessMergedBeanDefinition(RootBeanDefinition, UserService, UserService)

    8. ApplicationListenerDetector#postProcessMergedBeanDefinition(RootBeanDefinition, UserService, UserService)

    9. ImportAwareBeanPostProcessor#postProcessAfterInstantiation(UserService, UserService)

    10. AnnotationAwareAspectJAutoProxyCreator#postProcessAfterInstantiation(UserService, UserService)

    11. CommonAnnotationBeanPostProcessor#postProcessAfterInstantiation(UserService, UserService)

    12. AutowiredAnnotationBeanPostProcessor#postProcessAfterInstantiation(UserService, UserService)

    13. ImportAwareBeanPostProcessor#postProcessProperties(MutablePropertyValues, UserService, UserService)

    14. AnnotationAwareAspectJAutoProxyCreator#postProcessProperties(MutablePropertyValues, UserService, UserService)

    15. AnnotationAwareAspectJAutoProxyCreator#postProcessPropertyValues(MutablePropertyValues, PropertyDescriptor[], UserService, UserService)

    16. CommonAnnotationBeanPostProcessor#postProcessProperties(MutablePropertyValues, UserService, UserService)

    17. AutowiredAnnotationBeanPostProcessor#postProcessProperties(MutablePropertyValues, UserService, UserService)

    18. UserService#setBeanFactory(DefaultListableBeanFactory)

    19. LogBeanPostProcessor#postProcessBeforeInitialization(UserService, UserService)

    20. LogDestructionAwareBeanPostProcessor#postProcessBeforeInitialization(UserService, UserService)

    21. ApplicationContextAwareProcessor#postProcessBeforeInitialization(UserService, UserService)

    22. UserService#setApplicationContext(AnnotationConfigApplicationContext)

    23. ImportAwareBeanPostProcessor#postProcessBeforeInitialization(UserService, UserService)

    24. BeanPostProcessorChecker#postProcessBeforeInitialization(UserService, UserService)

    25. AnnotationAwareAspectJAutoProxyCreator#postProcessBeforeInitialization(UserService, UserService)

    26. CommonAnnotationBeanPostProcessor#postProcessBeforeInitialization(UserService, UserService)

    27. AutowiredAnnotationBeanPostProcessor#postProcessBeforeInitialization(UserService, UserService)

    28. ApplicationListenerDetector#postProcessBeforeInitialization(UserService, UserService)

    29. UserService#afterPropertiesSet()

    30. UserService#init()

    31. LogBeanPostProcessor#postProcessAfterInitialization(UserService, UserService)

    32. LogDestructionAwareBeanPostProcessor#postProcessAfterInitialization(UserService, UserService)

    33. ApplicationContextAwareProcessor#postProcessAfterInitialization(UserService, UserService)

    34. ImportAwareBeanPostProcessor#postProcessAfterInitialization(UserService, UserService)

    35. BeanPostProcessorChecker#postProcessAfterInitialization(UserService, UserService)

    36. AnnotationAwareAspectJAutoProxyCreator#postProcessAfterInitialization(UserService, UserService)

    37. CommonAnnotationBeanPostProcessor#postProcessAfterInitialization(UserService, UserService)

    38. AutowiredAnnotationBeanPostProcessor#postProcessAfterInitialization(UserService, UserService)

    39. ApplicationListenerDetector#postProcessAfterInitialization(UserService, UserService)

  21. 销毁 Bean,beanFactory.destroyBean(bean)

    1. LogDestructionAwareBeanPostProcessor#postProcessBeforeDestruction(UserService, UserService)

    2. UserService#destroy()

不知道有没有人关注这个附录日志,这里再重复一遍:在下一篇文章 Spring Bean 生命周期概述 中,D瓜哥将针对 Spring Bean 的整个生命周期展开详细说明。