概述
Spring 是我们最常用的框架之一,我们今天一起来带大家一些探究一下 Spring 的启动过程。
首先,Spring 的启动过程分为 12 个步骤主要是完成容器的初始化,以及对单实例非懒加载 Bean 完成创建和Bean 属性的赋值注入和初始化,以及消息派发器的创建和启动过程消息的触发。
补充:本文和后续版本基于 spring-5.1.14 版本展开
Spring 使用 Demo
public class DemoApplicationTest {
public static void main(String[] args) {
// 创建容器
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
// 获取 UserService 对象
UserService userService = applicationContext.getBean(UserService.class);
// 执行 test 方法
userService.test();
}
}
@Configuration
@Import(UserService.class)
class AppConfig {
}
// UserSerivce 类
@Service
public class UserService {
public String test() {
return "test";
}
}
Spring 的启动过程以及方法入口
// 入口方法
AbstractApplicationContext#refresh()
// 1. 刷新前的预处理工作
prepareRefresh();
// 2. 获取 BeanFactory
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// 3. BeanFactory 预处理工作
prepareBeanFactory(beanFactory);
// 4.BeanFactory 完成后进行的后置处理工作
postProcessBeanFactory(beanFactory);
// 5. 执行 BeanFactoryPostProcessors
invokeBeanFactoryPostProcessors(beanFactory);
// 6. 注册 Bean 后置处理器 [intercept bean creation.]
registerBeanPostProcessors(beanFactory);
// 7. 初始化 MessageSource 组件(做国际化功能, 消息绑定,消息解析)
initMessageSource();
// 8. 初始化事件派发器
initApplicationEventMulticaster();
// 9. 留给自容器(子类)
onRefresh();
// 10. 给容器中将所有的项目中的 ApplicationListener 注册进来
registerListeners();
// 11. 初始化所有的非懒加载单实例Bean
finishBeanFactoryInitialization(beanFactory);
// 12. 执行Spring容器的生命周期(启动)和发布事件
finishRefresh();
Spring 启动过程描述
1. prepareRefresh() 刷新前的预处理工作
- 记录启动事件
- 允许子容器可以设置一些属性到 environment
- 检查衍生属性是否合法,是否包含必须的属性
- 实现代码如下:
protected void prepareRefresh() {
// Switch to active.
this.startupDate = System.currentTimeMillis();
//容器是否关闭
this.closed.set(false);
//容器启动
this.active.set(true);
if (logger.isDebugEnabled()) {
if (logger.isTraceEnabled()) {
logger.trace("Refreshing " + this);
}
else {
logger.debug("Refreshing " + getDisplayName());
}
}
// Initialize any placeholder property sources in the context environment.
// 1. 初始化一些属性设置, 允许子容器设置一些内容到 environment 中
initPropertySources();
// Validate that all properties marked as required are resolvable:
// see ConfigurablePropertyResolver#setRequiredProperties
// 2. 校验必填属性是否有值
getEnvironment().validateRequiredProperties();
// Store pre-refresh ApplicationListeners...
if (this.earlyApplicationListeners == null) {
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
}
else {
// Reset local application listeners to pre-refresh state.
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}
// Allow for the collection of early ApplicationEvents,
// to be published once the multicaster is available...
// 3. 创建集合保存容器的一些早期事件
this.earlyApplicationEvents = new LinkedHashSet<>();
}
- 在Spring MVC 中对
initPropertySources方法做了实现,将 Servlet 容器相关的信息放到了 environment 中,实现如下
//Spring MVC 的 GenericWebApplicationContext 类
protected void initPropertySources() {
ConfigurableEnvironment env = getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, null);
}
}
2. obtainFreshBeanFactory() 获取 BeanFactory 对象
- 刷新 BeanFactory
// 创建 BeanFactory 是在 AbstractApplicationContext 的无参构造方法中初始化
// GenericApplicationContext 类
public GenericApplicationContext() {
this.beanFactory = new DefaultListableBeanFactory();
}
// AnnotationConfigApplicationContext 类的定义签名和继承关系
public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry {
// ......
}
- 调用
obtainFreshBeanFactory方法返回 BeanFactory
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
// 1. 刷新【创建】BeanFactory
refreshBeanFactory();
// 2. 返回 BeanFactory
return getBeanFactory();
}
- 注意:
refreshBeanFactory方法有两个实现类AbstractRefreshableApplicationContext,GenericApplicationContext我们当前创建容器是你用的AnnotationConfigApplicationContext类它是GenericApplicationContext的子类,所以当前容器 不支持重复刷新。如果需要重复刷新的话可以选择AnnotationConfigWebApplicationContext类。
AnnotationConfigApplicationContext application =new AnnotationConfigApplicationContext();
application.register(ApplicationConfig.class);
application.refresh();
application.refresh(); //报错: IllegalStateException
3. prepareBeanFactory(beanFactory) 准备 BeanFactory
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// 1. 设置 BeanFactory 的类加载器,支持表达式解析器 ....
beanFactory.setBeanClassLoader(getClassLoader());
// el 解析器
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
// 默认类型转换器
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// 2. 添加部分的 BeanFactory 的 BeanPostProcessor [ApplicationContextAwareProcessor]
// Bean 后置处理器
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
// 3. 设置忽略的自动装配的接口 EnvironmentAware 、EmbeddedValueResolverAware
// 如果实现了这些接口重写的 set 方法,那么 Spring 就不会去自动装配
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
// 4. 注册可以解析的自动装配,我们能够直接在任何组件中自动注入:
// BeanFactory、ResourceLoader、ApplicationEventPublisher、 ApplicationContext
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// 5. 添加 BeanPostProcessor 【ApplicationListenerDetector】
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
// 6. 添加编译时的 AspectJ 支持
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
// Set a temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
// 7. 给 BeanFactory 中注册一些能用的组件:
// environment 【ConfigurableEnvironment】
// systemProperties 【Map<String, Object>】
// systemEnvironment【Map<String, Object>】
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
}
4. postProcessBeanFactory(beanFactory)
- 本方法是用来给子类实现拓展的
5. invokeBeanFactoryPostProcessors(beanFactory) 执行 BeanFactory 后置处理器
1. 获取所有的 BeanDefinitionRegistryPostProcessor
2. 先执行实现了 PriorityOrdered 优先级接口BeanDefinitionRegistryPostProcessor
3. 再执行实现了 Ordered 顺序接口的 BeanDefinitionRegistryPostProcessor
4. 最后一步执行没有实现优先级接口或者顺序的接口的 BeanDefinitionRegistryPostProcessors
5. 获取所有的 BeanFactoryPostProcessor
6. 执行实现了 PriorityOrdered 优先级接口的 BeanFactoryPostProcessor
7. 执行实现了 Ordered 顺序接口的 BeanFactoryPostProcessor
8. 执行没有实现优先级接口或者顺序的接口的 BeanFactoryPostProcessor
/**
* 执行 BeanFactoryPostProcessors 的方法
* @param beanFactory
* @param beanFactoryPostProcessors
*/
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
// 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<>();
// 1. 获取所有的 BeanDefinitionRegistryPostProcessor
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
// 2. 先执行实现了 PriorityOrdered 优先级接口的 BeanDefinitionRegistryPostProcessor、
// postProcessor.postProcessBeanDefinitionRegistry(registry);
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);
currentRegistryProcessors.clear();
// 3. 再执行实现了 Ordered 顺序接口的 BeanDefinitionRegistryPostProcessor
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);
currentRegistryProcessors.clear();
// 4. 最后一步执行没有实现优先级接口或者顺序的接口的 BeanDefinitionRegistryPostProcessors
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);
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);
}
// 再来执行 BeanFactoryPostProcess 的方法
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);
}
}
// 2. 先执行实现了 PriorityOrdered 优先级接口的 BeanFactoryPostProcessor、
// postProcessor.postProcessBeanFactory(beanFactory);
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
// 3. 再执行实现了 Ordered 顺序接口的 BeanFactoryPostProcessor
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
// 4. 最后一步执行没有实现优先级接口或者顺序的接口的 BeanFactoryPostProcessor
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
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();
}
6. registerBeanPostProcessors(beanFactory) 注册 Bean 后置处理器
- BeanPostProcessor 进行排序,排序分为三类:PriorityOrdered, Ordered,non 默认,MergedBeanDefinitionPostProcessor
- 排序后以此存入到 priorityOrderedPostProcessors, orderedPostProcessors, nonOrderedPostProcessors,internalPostProcessors 集合中
- 这里的优先级是:
PriorityOrdered -> Ordered -> 无 -> MergedBeanDefinitionPostProcessor注意:如果是实现了多个接口那么则按照最低优先级作为排序的顺序 - 按照顺序注册后置处理器
- 最后注册一个 ApplicationListenerDetector 到容器中
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
// 1. 获取所有的 BeanPostProcessor; 后置处理器都可以通过 PriorityOrdered、Ordered 接口来指定优先级
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
// Register BeanPostProcessorChecker that logs an info message when
// a bean is created during BeanPostProcessor instantiation, i.e. when
// a bean is not eligible for getting processed by all BeanPostProcessors.
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
// Separate between BeanPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {// PriorityOrdered @Order @Priority
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
priorityOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
// 2. 先注册 PriorityOrdered 优先级接口的 BeanPostProcessors
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// 3. 再注册 Order 优先级接口的 BeanPostProcessors
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
for (String ppName : orderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// 4. 最后注册没有任何优先级接口的 BeanPostProcessors
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
for (String ppName : nonOrderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
// 5. 最终注册 MergedBeanDefinitionPostProcessor
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
// Re-register post-processor for detecting inner beans as ApplicationListeners,
// moving it to the end of the processor chain (for picking up proxies etc).
// 6. 注册一个 ApplicationListenerDetector,来在 Bean 创建完成后检查是否是 ApplicationListener 如果是
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
7. initMessageSource() 初始化 MessageSource 组件
8. initApplicationEventMulticaster() 初始化事件派发器
protected void initApplicationEventMulticaster() {
// 1. 获取 BeanFactory
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
// 2. 从 BeanFactory 中获取 applicationEventMulticaster 的 applicationEventMulticaster
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isTraceEnabled()) {
logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
else {
// 3. 如果上一步没有配置:创建一个 SimpleApplicationEventMulticaster
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
// 4. 将创建的 ApplicationEventMulticaster 添加到 BeanFactory 中,以后其他组件直接自动注入即可
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isTraceEnabled()) {
logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
}
}
}
9. onRefresh() 预留方法,用于自定义实现重写实现特殊 Bean 的处理
10. registerListeners() 注册监听器
protected void registerListeners() {
// Register statically specified listeners first.
// 1. 从容器中拿到所有的 ApplicationListener 组件
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let post-processors apply to them!
// 2. 每个监听器添加到事件派发器中
// getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
// 到这一步因为 FactoryBean 还没有调用 getObject() 方法生成 Bean 对象, 所以这里要根据类型查找一下 ApplicationListener 记录对应类型
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// Publish early application events now that we finally have a multicaster...
// 3. 派发之前步骤产生的事件
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
11. finishBeanFactoryInitialization(beanFactory) 刷新前的预处理工作
1. 初始化单实例 Bean
2. 对属性进行赋值
和部分将在 Bean 的初始化,依赖注入,循环依赖中拓展
12. finishRefresh() 执行 Bean 的生命周期启动和时间发布
protected void finishRefresh() {
// Clear context-level resource caches (such as ASM metadata from scanning).
clearResourceCaches();
// Initialize lifecycle processor for this context.
// 2. initLifecycleProcessor(); 初始化和生命周期有关的后置处理器
// 默认从容器中找是否有 lifecycleProcessor 的组件 【LifecycleProcessor】,如果没有就使用默认的生命周期组件
// new DefaultLifecycleProcessor();
// 加入容器中方便使用
// LifecycleProcessor 写一个实现类 LifecycleProcessor 的实现类,可以在BeanFactory 的方法进行调用
// void onRefresh();
// void onClose();
initLifecycleProcessor();
// Propagate refresh to lifecycle processor first.
// 3. 拿到生命周期处理器(BeanFactory)回调 onRefresh()),
getLifecycleProcessor().onRefresh();
// Publish the final event.
// 4. publishEvent(new ContextRefreshedEvent(this)); 发布容器刷新完成事件
publishEvent(new ContextRefreshedEvent(this));
// Participate in LiveBeansView MBean, if active.
// 5. LiveBeansView.registerApplicationContext(this);
LiveBeansView.registerApplicationContext(this);
}