Spring源码-SpringBoot-启动流程-refresh

总结摘要
SpringBoot启动流程refresh方法介绍

方法refresh

源码摘录

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

// org.springframework.context.support.AbstractApplicationContext#refresh
@Override
public void refresh() throws BeansException, IllegalStateException {
    synchronized (this.startupShutdownMonitor) {
        StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

        // Prepare this context for refreshing.
        // 准备工作。包括配置环境信息、配置监听器等。
        prepareRefresh();

        // Tell the subclass to refresh the internal bean factory.
        // 获取 beanFactory。默认使用之前步骤创建的`AnnotationConfigServletWebServerApplicationContext`。
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

        // Prepare the bean factory for use in this context.
        // 配置`BeanFactory`。包括环境信息,注册一批作为工具的 bean 等。
        prepareBeanFactory(beanFactory);

        try {
            // Allows post-processing of the bean factory in context subclasses.
            // Bean 工厂进行后处理。注册系统 BeanPostProcessor,Scope 等。
            postProcessBeanFactory(beanFactory);

            StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
            // Invoke factory processors registered as beans in the context.
            // 调用 BeanFactoryPostProcessor 接口方法。
            // 功能是扫描与注册 bean,包括用户自定义配置的 bean 和自动配置类。此时暂未实例化未使用的 bean。
            invokeBeanFactoryPostProcessors(beanFactory);

            // Register bean processors that intercept bean creation.
            // 注册`BeanPostProcessor`。
            registerBeanPostProcessors(beanFactory);
            beanPostProcess.end();

            // Initialize message source for this context.
            // 初始化`MessageSource`。默认使用之前步骤中自动配置方式创建的`ResourceBundleMessageSource`。
            initMessageSource();

            // Initialize event multicaster for this context.
            // 初始化`ApplicationEventMulticaster`。默认主动创建`SimpleApplicationEventMulticaster`。
            initApplicationEventMulticaster();

            // Initialize other special beans in specific context subclasses.
            // 刷新 context。
            // 主要功能是初始化`ThemeSource`和创建`WebServer`。
            onRefresh();

            // Check for listener beans and register them.
            // 注册`ApplicationListener`。
            registerListeners();

            // Instantiate all remaining (non-lazy-init) singletons.
            // 实例化所有非懒加载的单例 bean。
            finishBeanFactoryInitialization(beanFactory);

            // Last step: publish corresponding event.
            // 完成`refresh`操作,发布`ContextRefreshedEvent`事件。
            // 主要功能包括启动`LifecycleProcessor`(start 方法),
            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();
            contextRefresh.end();
        }
    }
}

主流程详情

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
--> org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#refresh
    --> org.springframework.context.support.AbstractApplicationContext#refresh
        --> org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#prepareRefresh
            --> org.springframework.context.support.AbstractApplicationContext#prepareRefresh
                --> org.springframework.web.context.support.GenericWebApplicationContext#initPropertySources
                    --> org.springframework.web.context.support.StandardServletEnvironment#initPropertySources
                        --> 初始化`servletContextInitParams``servletConfigInitParams`两类配置信息默认无org.springframework.web.context.support.WebApplicationContextUtils#initServletPropertySources(org.springframework.core.env.MutablePropertySources, javax.servlet.ServletContext, javax.servlet.ServletConfig)
                --> getEnvironment().validateRequiredProperties();
                    --|> 校验必须的配置信息`org.springframework.core.env.AbstractPropertyResolver#requiredProperties`是否已存在默认无需该类信息校验通过
                    --> org.springframework.core.env.AbstractEnvironment#validateRequiredProperties
                        --> org.springframework.core.env.AbstractPropertyResolver#validateRequiredProperties
                --> `this.applicationListeners`作为`this.earlyApplicationListeners`。
        --> `obtainFreshBeanFactory();`
            --|> 从上下文获取`BeanFactory`,具体是`org.springframework.context.support.GenericApplicationContext#beanFactory`。
            --|> org.springframework.context.support.AbstractApplicationContext#obtainFreshBeanFactory
                --> org.springframework.context.support.GenericApplicationContext#refreshBeanFactory
                    --|> `BeanFactory``new AnnotationConfigServletWebServerApplicationContext();`命令的默认构造方法中创建的
                    --|> 具体是在`org.springframework.context.support.GenericApplicationContext#GenericApplicationContext()`中执行`new DefaultListableBeanFactory();`。
                    --|> 补充说明:`AnnotationConfigServletWebServerApplicationContext`的无参数构造方法中创建了`new AnnotatedBeanDefinitionReader``new ClassPathBeanDefinitionScanner`。
        --> `prepareBeanFactory(beanFactory);`
            --|> 配置`BeanFactory`。
            --> 配置`org.springframework.beans.factory.support.AbstractBeanFactory#beanExpressionResolver`,执行`new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())`。
            --> 配置`org.springframework.beans.factory.support.AbstractBeanFactory#propertyEditorRegistrars`,执行`new ResourceEditorRegistrar(this, getEnvironment())`。        
            --> `new ApplicationContextAwareProcessor(this))`,添加系统`BeanPostProcessor`。
            --> 配置若干`ignoreDependencyInterface`。
            --> 注册若干`ResolvableDependency`。
            --> `new ApplicationListenerDetector(this)`,添加系统`BeanPostProcessor`。
            --> `LoadTimeWeaver`相关配置
            --> 注册若干 bean包括`environment`,`systemProperties`,`systemEnvironment`,`applicationStartup`。
        --> `postProcessBeanFactory(beanFactory);`。
            --|> 允许在上下文子类中对 Bean 工厂进行后处理
            --|> org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#postProcessBeanFactory
            --> org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#postProcessBeanFactory
                --> `new WebApplicationContextServletContextAwareProcessor(this)`,添加系统`BeanPostProcessor`。
                --> 配置1个`ignoreDependencyInterface`(`ServletContextAware.class`)。
                --> org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#registerWebApplicationScopes
                    --> 检查当前 BeanFactory 中已存在的 scope 并缓存当前默认为空
                    --> org.springframework.web.context.support.WebApplicationContextUtils#registerWebApplicationScopes(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
                        --> 默认注册2个 scope
                            --|> 名称`request`对应`org.springframework.web.context.request.RequestScope`。
                            --|> 名称`session`对应`org.springframework.web.context.request.SessionScope`。
                        --> 当前`ServletContext`为空如果不为空则会新注册一个 scope `application`。
                        --> 注册若干`ResolvableDependency`,与新注册的 scope 有关
                    --> 如果之前 BeanFactory 中已存在并缓存的 scope将其重新注册以保证其优先级高于先前步骤默认注册的 scope
            --> 如果已注册`basePackages`,则调用`this.scanner.scan`扫描和注册 bean默认无
                --|> 关联提前注册方法`org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#scan`。
            --> 如果已注册`annotatedClasses`,则调用`this.reader.registern`扫描和注册 bean默认无
                --|> 关联提前注册方法`org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#register`。
        --> `invokeBeanFactoryPostProcessors(beanFactory);`。
            --|> org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors
            --> org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.List<org.springframework.beans.factory.config.BeanFactoryPostProcessor>)
                --|> 默认`beanFactoryPostProcessors`有3个其中2个是`BeanDefinitionRegistryPostProcessor`,第3个是`SpringApplication$PropertySourceOrderingBeanFactoryPostProcessor`。
                --> for-each beanFactoryPostProcessors处理`BeanDefinitionRegistryPostProcessor`实现类默认有2个如下
                    --|> SharedMetadataReaderFactoryContextInitializer$CachingMetadataReaderFactoryPostProcessor
                        --|> 注册 bean `SharedMetadataReaderFactoryBean.class`。
                        --|> 为名为`org.springframework.context.annotation.internalConfigurationAnnotationProcessor` bean 配置`metadataReaderFactory`,
                    --|> ConfigurationWarningsApplicationContextInitializer$ConfigurationWarningsPostProcessor
                        --|> 打印已存在的指定警告信息
                --> 第1次`beanFactory`中获取`BeanDefinitionRegistryPostProcessor`实现类缓存到`postProcessorNames`,默认1个
                    --|> `org.springframework.context.annotation.internalConfigurationAnnotationProcessor`
                --> for-each postProcessorNames匹配`PriorityOrdered.class`的实现类进行实例化并缓存到`currentRegistryProcessors`。
                    --> beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)
                    --> 筛选得到 1 ,`internalConfigurationAnnotationProcessor`名称对应的`ConfigurationClassPostProcessor`。
                --> 排序。`org.springframework.context.support.PostProcessorRegistrationDelegate#sortPostProcessors`。
                --> 使用`currentRegistryProcessors`注册 bean。`org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanDefinitionRegistryPostProcessors`
                    --> for-each currentRegistryProcessors调用接口方法`org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry`。
                        --> ConfigurationClassPostProcessor
                            --> org.springframework.context.annotation.ConfigurationClassPostProcessor#processConfigBeanDefinitions
                            --|> 该方法的逻辑较为复杂在另外的篇章中介绍
                --> 清空缓存`currentRegistryProcessors`。
                --> 第2次`beanFactory`中获取`BeanDefinitionRegistryPostProcessor`实现类缓存到`postProcessorNames`,默认2个
                    --> 已解析过org.springframework.context.annotation.internalConfigurationAnnotationProcessor
                    --> 此为用户自定义声明的 mybatis 相关的注解`@MapperScan`。tech.gdev.springbasicexplore.App#MapperScannerRegistrar#0
                --> for-each postProcessorNames匹配`Ordered.class`的实现类进行实例化并缓存到`currentRegistryProcessors`。
                    --> 筛选得到 0 
                --> 排序。`org.springframework.context.support.PostProcessorRegistrationDelegate#sortPostProcessors`。
                    --|> 参见前文
                --> 使用`currentRegistryProcessors`注册 bean。`org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanDefinitionRegistryPostProcessors`
                    --|> 具体参见前文
                --> 清空缓存`currentRegistryProcessors`。
                --> while 循环
                    --> 具体逻辑与第12次相同区别在于不再以`PriorityOrdered``Ordered`为条件过滤
                    --> 因用户自定义声明的 mybatis 相关的注解`@MapperScan`,因此可以扫描到新的 bean 定义
                --> invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
                    --|> 使用`registryProcessors`,即特殊的`BeanDefinitionRegistryPostProcessor`的实现类调用接口方法`org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory`。
                    --> SharedMetadataReaderFactoryContextInitializer$CachingMetadataReaderFactoryPostProcessor 空逻辑跳过
                    --> ConfigurationWarningsApplicationContextInitializer$ConfigurationWarningsPostProcessor 空逻辑跳过
                    --> ConfigurationClassPostProcessor 
                        --|> 增强`ConfigurationClasses`,详情参见介绍`ConfigurationClassPostProcessor`的其它篇章
                        --|> 注册`ImportAwareBeanPostProcessor`,`new ImportAwareBeanPostProcessor(beanFactory)`。
                    --> MapperScannerConfigurer 空逻辑跳过                   
                --> invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
                    --|> 使用`regularPostProcessors`,即普通的`BeanFactoryPostProcessor`的实现类调用接口方法`org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory`。
                    --> SpringApplication$PropertySourceOrderingBeanFactoryPostProcessor
                --> `beanFactory`中获取`BeanFactoryPostProcessor`实现类缓存到`postProcessorNames`,默认1个
                    --|> priorityOrderedPostProcessors = {ArrayList@7019}  size = 1
                    --|> 0 = {PropertySourcesPlaceholderConfigurer@7041} 
                    --|> orderedPostProcessorNames = {ArrayList@7021}  size = 2
                    --|> 0 = "emBeanDefinitionRegistrarPostProcessor"
                    --|> 1 = "org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer$DependsOnDatabaseInitializationPostProcessor"
                    --|> nonOrderedPostProcessorNames = {ArrayList@7022}  size = 2
                    --|> 0 = "org.springframework.context.event.internalEventListenerProcessor"
                    --|> 1 = "preserveErrorControllerTargetClassPostProcessor"
                    
        --> 注册`BeanPostProcessor`。org.springframework.context.support.AbstractApplicationContext#registerBeanPostProcessors
            --> org.springframework.context.support.PostProcessorRegistrationDelegate#registerBeanPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.context.support.AbstractApplicationContext)
                --> 根据 bean 类型在 beanFactory 中查找`BeanPostProcessor`。 
                --> 注册1个`BeanPostProcessor`,`new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount)`,作用大意是打印日志
                -->  bean 分为3类分别是实现`PriorityOrdered`实现`Ordered`和剩余的普通的普通`BeanPostProcessor`。
                --> 处理实现`PriorityOrdered` bean
                    --> 实例化实现`PriorityOrdered` bean缓存其中实现`MergedBeanDefinitionPostProcessor` bean 到局部变量`internalPostProcessors`。
                    --> 排序
                    -->  beanFactory 中将其注册为 BeanPostProcessor即缓存在实例变量`AbstractBeanFactory#beanPostProcessors`区别于注册 bean
                --> `PriorityOrdered`的处理方式注册其它2类 bean
                --> 重新注册局部变量`internalPostProcessors`中的 bean  BeanPostProcessor即将其优先级提到最高
                --> 注册`ApplicationListenerDetector`。`new ApplicationListenerDetector(applicationContext)`。
                --> 最终的 BeanPostProcessor 实例默认有约20个
        --> 初始化`MessageSource`。
            --|> org.springframework.context.support.AbstractApplicationContext#initMessageSource
            --> 优先根据名称`messageSource` beanFactory 中查找如果可以查找到则直接使用
                --|> 默认可以找到
                --|>  bean 为自动配置。`org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration#messageSource`。
                --|> ResourceBundleMessageSource
            --> 否则主动创建`DelegatingMessageSource`,填充 parent context  MessageSource 属性注册到 beanFactory 。`new DelegatingMessageSource()`。
        --> 初始化`ApplicationEventMulticaster`。
            --|> org.springframework.context.support.AbstractApplicationContext#initApplicationEventMulticaster
            --> 优先根据名称`applicationEventMulticaster` beanFactory 中查找如果可以查找到则直接使用
            --> 否则主动创建`SimpleApplicationEventMulticaster`注册到 beanFactory 。。`new SimpleApplicationEventMulticaster(beanFactory);`。
                --|> 默认找不到主动创建
        --> 刷新 context
            --|> `org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#onRefresh`
            --> 调用父类的方法初始化`ThemeSource`。`org.springframework.web.context.support.GenericWebApplicationContext#onRefresh`
                --> 初始化`ThemeSource`并填充到实例变量`GenericWebApplicationContext#themeSource`
                    --|> `org.springframework.ui.context.support.UiApplicationContextUtils#initThemeSource`
                    --> 优先根据名称`themeSource` beanFactory 中查找如果可以查找到则直接使用
                    --> 否则如果存在 parent context主动创建`DelegatingThemeSource`委托给 parent context
                    --> 否则创建`ResourceBundleThemeSource`。`new ResourceBundleThemeSource();`
            --> 创建`WebServer`。`org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#createWebServer`。 
                --> 如果当前不存在 webServer 且不存在 servletContext则创建
                    --|> 默认需要创建
                    --|> 创建并启动 Tomcat
                    --> 待补充
                --> 如果当前已存在 webServer  servletContext 不为空则仅初始化。`getSelfInitializer().onStartup(servletContext);`。 
                    --|> 
                --> 初始化`PropertySource`。
                    --|> org.springframework.web.context.support.GenericWebApplicationContext#initPropertySources
        --> 注册`ApplicationListener`。
            --> `AbstractApplicationContext#applicationListeners`中的监听器注册到`AbstractApplicationContext#applicationEventMulticaster`
                --|> 默认有14个相比``增加了2个分别是`SharedMetadataReaderFactoryBean``ScheduledAnnotationBeanPostProcessor`。
            --> 根据 bean 类型在 beanFactory 中查找`ApplicationListener`,此处仅查询 bean name不会提前实例化对象 bean name 注册到`AbstractApplicationContext#applicationEventMulticaster`
                --|> 默认有8个
                --> 0 = "&org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory"
                --> 1 = "listenerBean"
                --> 2 = "org.springframework.context.annotation.internalScheduledAnnotationProcessor"
                --> 3 = "mvcResourceUrlProvider"
                --> 4 = "applicationAvailability"
                --> 5 = "startupTimeMetrics"
                --> 6 = "tomcatMetricsBinder"
                --> 7 = "springApplicationAdminRegistrar"
            --> 如果`AbstractApplicationContext#earlyApplicationEvents`中存在事件则使用`AbstractApplicationContext#applicationEventMulticaster`进行发布可能会涉及到提前实例化监听器 bean
                --|> 默认不存在事件无需发布
        --> 实例化所有非懒加载的单例 bean。`finishBeanFactoryInitialization(beanFactory);`。
            --|> org.springframework.context.support.AbstractApplicationContext#finishBeanFactoryInitialization
            --> 如果 beanFactory 未配置 conversionService 属性则进行配置默认已完成配置
            --> 如果 beanFactory 未配置 EmbeddedValueResolver则进行配置默认已完成配置
            --> `LoadTimeWeaverAware`相关处理
                --> 此处涉及1个`LoadTimeWeaverAware`实现其名称是`&entityManagerFactory`。
            --> 重置 beanFactory `TempClassLoader` null
            --> 标记冻结 beanFactory 中的 bean 定义。`beanFactory.freezeConfiguration()`
            --> 预创建非懒加载的单例 bean
                --|> org.springframework.beans.factory.support.DefaultListableBeanFactory#preInstantiateSingletons
                --|> 待补充详情
        --> 完成`refresh`操作发布相应事件
            --|> org.springframework.context.support.AbstractApplicationContext#finishRefresh
            --> 清理 context 级别的缓存
            --> 初始化`LifecycleProcessor`。 
                --|> org.springframework.context.support.AbstractApplicationContext#initLifecycleProcessor
                -->  beanFactory 中根据名称`lifecycleProcessor`查找`LifecycleProcessor`。
                    --|> 默认已存在该 bean
                    --|> 自动配置类路径`org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration#defaultLifecycleProcessor`。
                        --> 默认配置属性`DefaultLifecycleProcessor#timeoutPerShutdownPhase`为30s
                        --|> Specify the maximum time allotted in milliseconds for the shutdown of any phase (group of SmartLifecycle beans with the same 'phase' value). The default value is 30000 milliseconds (30 seconds).
                        --|> 指定任何阶段具有相同phase值的 SmartLifecycle bean 关闭的最大允许时间以毫秒为单位)。默认值为 30000 毫秒30 )。
                --> 如果未找到则创建`DefaultLifecycleProcessor`并注册到 beanFactory 。`new DefaultLifecycleProcessor();`
            --> refresh LifecycleProcessor
                --|> org.springframework.context.support.DefaultLifecycleProcessor#onRefresh
                --> 启动 bean。`startBeans(true);`
                    --|> org.springframework.context.support.DefaultLifecycleProcessor#startBeans
                    --> 获取`Lifecycle`类型的 bean
                        --|> org.springframework.context.support.DefaultLifecycleProcessor#getLifecycleBeans
                        --> 根据类型在 beanFactory 中查询`Lifecycle`。
                        --> 默认有3个查询到4个 bean name其中1个是本身this),跳过该 bean
                        --> "springBootLoggingLifecycle" -> {LoggingApplicationListener$Lifecycle@10501} 
                        --> "webServerGracefulShutdown" -> {WebServerGracefulShutdownLifecycle@10502} 
                        --> "webServerStartStop" -> {WebServerStartStopLifecycle@10503} 
                    --> 根据`Phase`将其分组为若干个`LifecycleGroup`。
                    --> 遍历上一步骤的分组启动分组
                        --|> org.springframework.context.support.DefaultLifecycleProcessor.LifecycleGroup#start
                        --> 排序 members in group然后遍历启动
                            --> org.springframework.context.support.DefaultLifecycleProcessor#doStart
                            --|> 先启动依赖对象再启动自己涉及状态判断等
                        --> LoggingApplicationListener 
                            --|> 更新`LoggingApplicationListener.Lifecycle#running` true
                        --> WebServerGracefulShutdownLifecycle 
                            --|> 启动 WebServer
                            --> org.springframework.boot.web.embedded.tomcat.TomcatWebServer#start
                            --> 发布`ServletWebServerInitializedEvent`事件该事件无响应
                        --> WebServerStartStopLifecycle
                            --|> 更新`WebServerGracefulShutdownLifecycle#running` true
            --> 发布`ContextRefreshedEvent`事件

调用 BeanFactoryPostProcessor 接口方法详情

源码摘录

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
public static void invokeBeanFactoryPostProcessors(
        ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

    // WARNING: Although it may appear that the body of this method can be easily
    // refactored to avoid the use of multiple loops and multiple lists, the use
    // of multiple lists and multiple passes over the names of processors is
    // intentional. We must ensure that we honor the contracts for PriorityOrdered
    // and Ordered processors. Specifically, we must NOT cause processors to be
    // instantiated (via getBean() invocations) or registered in the ApplicationContext
    // in the wrong order.
    //
    // Before submitting a pull request (PR) to change this method, please review the
    // list of all declined PRs involving changes to PostProcessorRegistrationDelegate
    // to ensure that your proposal does not result in a breaking change:
    // https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22

    // 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<>();

        for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
            if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                BeanDefinitionRegistryPostProcessor registryProcessor =
                        (BeanDefinitionRegistryPostProcessor) postProcessor;
                registryProcessor.postProcessBeanDefinitionRegistry(registry);
                registryProcessors.add(registryProcessor);
            }
            else {
                regularPostProcessors.add(postProcessor);
            }
        }

        // 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.
        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);
            }
        }
        sortPostProcessors(currentRegistryProcessors, beanFactory);
        registryProcessors.addAll(currentRegistryProcessors);
        invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
        currentRegistryProcessors.clear();

        // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
        postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        for (String ppName : postProcessorNames) {
            if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
                currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                processedBeans.add(ppName);
            }
        }
        sortPostProcessors(currentRegistryProcessors, beanFactory);
        registryProcessors.addAll(currentRegistryProcessors);
        invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
        currentRegistryProcessors.clear();

        // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
        boolean reiterate = true;
        while (reiterate) {
            reiterate = false;
            postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                if (!processedBeans.contains(ppName)) {
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                    reiterate = true;
                }
            }
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            registryProcessors.addAll(currentRegistryProcessors);
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
            currentRegistryProcessors.clear();
        }

        // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
        invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
        invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
    }

    else {
        // Invoke factory processors registered with the context instance.
        invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
    }

    // Do not initialize FactoryBeans here: We need to leave all regular beans
    // uninitialized to let the bean factory post-processors apply to them!
    String[] postProcessorNames =
            beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

    // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
    // Ordered, and the rest.
    List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
    List<String> orderedPostProcessorNames = new ArrayList<>();
    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
    for (String ppName : postProcessorNames) {
        if (processedBeans.contains(ppName)) {
            // skip - already processed in first phase above
        }
        else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
        }
        else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
            orderedPostProcessorNames.add(ppName);
        }
        else {
            nonOrderedPostProcessorNames.add(ppName);
        }
    }

    // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
    invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

    // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
    List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
    for (String postProcessorName : orderedPostProcessorNames) {
        orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    }
    sortPostProcessors(orderedPostProcessors, beanFactory);
    invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

    // Finally, invoke all other BeanFactoryPostProcessors.
    List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
    for (String postProcessorName : nonOrderedPostProcessorNames) {
        nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    }
    invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

    // Clear cached merged bean definitions since the post-processors might have
    // modified the original metadata, e.g. replacing placeholders in values...
    beanFactory.clearMetadataCache();
}

流程详情

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 调用 BeanFactoryPostProcessor 接口方法详情
// org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.List<org.springframework.beans.factory.config.BeanFactoryPostProcessor>)

概述
--> 获取当前已注册并分类的`BeanFactoryPostProcessor`,`org.springframework.context.support.AbstractApplicationContext#beanFactoryPostProcessors`变量中缓存的 bean
    --|> 此处区别于已注册到 beanFactory 但是未分类的 bean即可以根据 bean 类型在 beanFactory 中找到但是未缓存在`AbstractApplicationContext#beanFactoryPostProcessors`变量中的 bean
    --|> 此步骤得到的`BeanFactoryPostProcessor`通常均为系统`BeanFactoryPostProcessor`,来源于硬编码或自动配置均为直接`new`方法创建的实例
--> 从上一步骤获取到`BeanFactoryPostProcessor`中过滤出`BeanDefinitionRegistryPostProcessor`,调用这一批 bean 的接口方法`BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry`。
    --|> 此类的`BeanFactoryPostProcessor`优先级最高
    --|> 此步骤允许继续注册 bean definition
    --|> 排序规则为`org.springframework.core.annotation.AnnotationAwareOrderComparator#INSTANCE`。
    --|> 其余非`BeanDefinitionRegistryPostProcessor`的普通`BeanFactoryPostProcessor`缓存到局部变量`regularPostProcessors`在本方法后续逻辑中处理
--> 根据 bean 类型在 beanFactory 中查找实现`PriorityOrdered``BeanDefinitionRegistryPostProcessor`,进行实例化并排序然后调用接口方法`BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry`。
    --|> 此步骤唯一处理`internalConfigurationAnnotationProcessor`名称对应的`ConfigurationClassPostProcessor`,该类是 bean 扫描和注册的核心类将在 beanFactory 中注册大量的 bean
    --|> 实例化的方式是调用`org.springframework.beans.factory.BeanFactory#getBean(java.lang.String, java.lang.Class<T>)`,通常会执行 beanFactory 的完整生命周期
    --|> 这些 bean 通常是以 bean definition 形式而非已创建的实例注册到 beanFactory 将由 beanFactory 进行实例化
    --|> 此步骤是 beanFactory 中的第1次查找
--> 根据 bean 类型在 beanFactory 中查找实现`Ordered``BeanDefinitionRegistryPostProcessor`,进行实例化并排序然后调用接口方法`BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry`。
    --|> 默认未找到新的`BeanDefinitionRegistryPostProcessor`。
    --|> 处理逻辑与`PriorityOrdered``BeanDefinitionRegistryPostProcessor`的处理方式相同
    --|> 此步骤是 beanFactory 中的第2次查找
--> 循环方式查询到 beanFactory 中的所有普通`BeanDefinitionRegistryPostProcessor`。
    --> 在每个循环中根据 bean 类型在 beanFactory 中查找BeanDefinitionRegistryPostProcessor`,进行实例化并排序然后调用接口方法`BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry`。
    --> 如果无新增的`BeanDefinitionRegistryPostProcessor`,则退出循环
    --|> 此步骤是 beanFactory 中的第3次查找可能查找若干次
--> 实现了`BeanDefinitionRegistryPostProcessor``BeanFactoryPostProcessor`调用接口方法`BeanFactoryPostProcessor#postProcessBeanFactory`。
--> 普通的即未实现`BeanDefinitionRegistryPostProcessor``BeanFactoryPostProcessor`调用接口方法`BeanFactoryPostProcessor#postProcessBeanFactory`。
--> 根据 bean 类型在 beanFactory 中查找所有`BeanFactoryPostProcessor`,暂不实例化
--> 实例化实现`PriorityOrdered``BeanFactoryPostProcessor`,排序后调用接口方法`BeanFactoryPostProcessor#postProcessBeanFactory`。
--> 实例化实现`Ordered``BeanFactoryPostProcessor`,排序后调用接口方法`BeanFactoryPostProcessor#postProcessBeanFactory`。
--> 实例化普通的`BeanFactoryPostProcessor`,排序后调用接口方法`BeanFactoryPostProcessor#postProcessBeanFactory`。


每种`BeanDefinitionRegistryPostProcessor`仅支持注册优先级低于本身的`BeanDefinitionRegistryPostProcessor`,否则优先级无法正确发挥作用
实现`PriorityOrdered``BeanDefinitionRegistryPostProcessor`允许注册实现实现`Ordered`的和普通的`BeanDefinitionRegistryPostProcessor`;如果注册同级的实现`PriorityOrdered` bean会被视为实现`Ordered` bean接口`PriorityOrdered`继承自`Ordered`)。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
AbstractApplicationContext#registerBeanPostProcessors

过程中各类 BeanPostProcessor 有序排序如下
internalPostProcessors = {ArrayList@5269}  size = 4
 0 = {PersistenceAnnotationBeanPostProcessor@5387} 
 1 = {CommonAnnotationBeanPostProcessor@5301} 
 2 = {AutowiredAnnotationBeanPostProcessor@5286} 
 3 = {ScheduledAnnotationBeanPostProcessor@5913} 
priorityOrderedPostProcessors = {ArrayList@5267}  size = 4
 0 = {ConfigurationPropertiesBindingPostProcessor@5386} 
 1 = {PersistenceAnnotationBeanPostProcessor@5387} 
 2 = {CommonAnnotationBeanPostProcessor@5301} 
 3 = {AutowiredAnnotationBeanPostProcessor@5286} 
orderedPostProcessors = {ArrayList@5383}  size = 5
 0 = {AnnotationAwareAspectJAutoProxyCreator@5454} "proxyTargetClass=true; optimize=false; opaque=false; exposeProxy=false; frozen=false"
 1 = {AsyncAnnotationBeanPostProcessor@5530} "proxyTargetClass=false; optimize=false; opaque=false; exposeProxy=false; frozen=false"
 2 = {ScheduledAnnotationBeanPostProcessor@5913} 
 3 = {FilteredMethodValidationPostProcessor@5926} "proxyTargetClass=true; optimize=false; opaque=false; exposeProxy=false; frozen=false"
 4 = {PersistenceExceptionTranslationPostProcessor@5927} "proxyTargetClass=true; optimize=false; opaque=false; exposeProxy=false; frozen=false"
nonOrderedPostProcessors = {ArrayList@5908}  size = 6
 0 = {WebServerFactoryCustomizerBeanPostProcessor@5930} 
 1 = {ErrorPageRegistrarBeanPostProcessor@5931} 
 2 = {HealthEndpointConfiguration$HealthEndpointGroupsBeanPostProcessor@5932} 
 3 = {MeterRegistryPostProcessor@5933} 
 4 = {MetricsRepositoryMethodInvocationListenerBeanPostProcessor@5934} 
 5 = {ProjectingArgumentResolverRegistrar$ProjectingArgumentResolverBeanPostProcessor@5935} 

完成全部注册后的 BeanPostProcessor 有序排序如下
org.springframework.beans.factory.support.AbstractBeanFactory#beanPostProcessors
0 = {ApplicationContextAwareProcessor@5944} 
1 = {WebApplicationContextServletContextAwareProcessor@5945} 
2 = {ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor@5946} 
3 = {PostProcessorRegistrationDelegate$BeanPostProcessorChecker@5947} 
4 = {ConfigurationPropertiesBindingPostProcessor@5386} 
5 = {AnnotationAwareAspectJAutoProxyCreator@5454} "proxyTargetClass=true; optimize=false; opaque=false; exposeProxy=false; frozen=false"
6 = {AsyncAnnotationBeanPostProcessor@5530} "proxyTargetClass=false; optimize=false; opaque=false; exposeProxy=false; frozen=false"
7 = {FilteredMethodValidationPostProcessor@5926} "proxyTargetClass=true; optimize=false; opaque=false; exposeProxy=false; frozen=false"
8 = {PersistenceExceptionTranslationPostProcessor@5927} "proxyTargetClass=true; optimize=false; opaque=false; exposeProxy=false; frozen=false"
9 = {WebServerFactoryCustomizerBeanPostProcessor@5930} 
10 = {ErrorPageRegistrarBeanPostProcessor@5931} 
11 = {HealthEndpointConfiguration$HealthEndpointGroupsBeanPostProcessor@5932} 
12 = {MeterRegistryPostProcessor@5933} 
13 = {MetricsRepositoryMethodInvocationListenerBeanPostProcessor@5934} 
14 = {ProjectingArgumentResolverRegistrar$ProjectingArgumentResolverBeanPostProcessor@5935} 
15 = {PersistenceAnnotationBeanPostProcessor@5387} 
16 = {CommonAnnotationBeanPostProcessor@5301} 
17 = {AutowiredAnnotationBeanPostProcessor@5286} 
18 = {ScheduledAnnotationBeanPostProcessor@5913} 
19 = {ApplicationListenerDetector@5937} 

END