在 SpringBoot 启动流程中,主要的两个阶段是初始化 SpringApplication 对象以及 SpringApplication.run 方法执行的内容,今天主要细讲的是 SpringApplication.run 中的刷新容器 refreshContext 方法,refreshContext 的源码如下
refreshContext 方法主要是刷新容器,下面我们来看一下 refreshContext 的源码,点击 SpringApplication.run 方法的 refreshContext 方法
看到 refreshContext 的方法内容很简单,主要是调用 refresh 方法
继续点击 refresh 方法可以看到,Refresh the underlying {@link ApplicationContext}也就是刷新底层的 ApplicationContext
继续跟进去,这里要选择 AbstractApplicationContext
这里我们看一下 AbstractApplicationContext 的注释,注释内容
Abstract implementation of the {@link org.springframework.context.ApplicationContext} interface. Doesn’t mandate the type of storage used for configuration; simply implements common context functionality. Uses the Template Method design pattern,requiring concrete subclasses to implement abstract methods.翻译过来就是当前抽象类是 ApplicationContext 接口的抽象实现,不强制要求用于配置的存储类型;它只是实现了公共上下文功能,使用的是模板方法的设计模式,需要具体的子类来实现抽象方法。下面我们再来看 refresh 方法
refresh 方法主要是刷新应用程序上下文,这里主要涉及到准备刷新上下文,调用上下文注册为 bean 的工厂处理器,初始化上下文的消息源,初始化特定上下文子类中的其他特殊 bean,检查监听器 bean 并注册,最后发布相应的事件并销毁已经创建的单例及重置 active 标志,整体的注解我都直接加在源码中了
@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. 告诉子类刷新内部bean工厂
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context. 准备bean工厂以用于此上下文
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses. 允许在上下文子类中对bean工厂进行后置处理
postProcessBeanFactory(beanFactory);
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
// Invoke factory processors registered as beans in the context. 调用上下文中注册为bean的工厂处理器
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation. 注册拦截器bean创建的bean处理器
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
// Initialize message source for this context. 初始化此上下文的消息源
initMessageSource();
// Initialize event multicaster for this context. 为此上下文初始化事件多播
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses. 初始化特定上下文子类中的其他特殊bean
onRefresh();
// Check for listener beans and register them. 检查监听器bean并注册
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons. 实例化所有剩余的(非懒惰初始化)单例
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event. 最后一步:发布相应的事件
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources. 销毁已创建的单例以避免悬空资源
destroyBeans();
// Reset 'active' flag. 重置active标志
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...
//重置Spring核心中的公共内省缓存,因为我们可能不再需要单例bean的元数据
resetCommonCaches();
contextRefresh.end();
}
}
}
到这里 SpringBoot 启动流程中的 refreshContext 方法的主体源码内容已经讲解完了,后续我们继续深入来看 refresh 方法内部子方法的源码,敬请关注。