工作如何繁忙,生活如何糟心,至少求知的这一刻是我的…
前言
基于Spring Boot 2.3.1。
刷新上下文做了什么?
private void refreshContext(ConfigurableApplicationContext context) {
refresh((ApplicationContext) context);
if (this.registerShutdownHook) {
try {
context.registerShutdownHook();
}
catch (AccessControlException ex) {
// Not allowed in some environments.
}
}
}
// 调用到这个重载方法。
@Deprecated
protected void refresh(ApplicationContext applicationContext) {
Assert.isInstanceOf(ConfigurableApplicationContext.class, applicationContext);
refresh((ConfigurableApplicationContext) applicationContext);
}
// 最终调用到这个重载方法。
protected void refresh(ConfigurableApplicationContext applicationContext) {
applicationContext.refresh();
}
applicationContext.refresh()方法有多种实现,有AbstractApplicationContext,ReactiveWebServerApplicationContext,ServletWebServerApplicationContext。显然,后两类都继承第一个类,同时,其余两个类调用的都是super.refresh(),实质上都调用到了AbstractApplicationContext#refresh。因此先看抽象类中的refresh方法做了哪些事。
官方给出的注释已经把大致步骤说得比较清晰了,哪里存在疑问的可以一个个查看其实现类:
// AbstractApplicationContext.class
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 记录下context的启动时间,并且记录下刷新前的所有监听器,清空刷新前的所有事件。
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
// 重新生成下beanFactory的serializationId,对于一些需要序列化的场景,需要这么做,对整体流程无明显影响。
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.
// 执行beanFactory的后处理,比如我的工程是web工程,这里注册了两个scope: session, request。
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
// 原来的beanPostProcessors作为工厂方法的postProcessor,现全部注册到beanPostProcessor列表中。
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
// 初始化messageSource,和语言环境相关,Spring Boot通过它完成国际化。
initMessageSource();
// Initialize event multicaster for this context.
// 初始化事件广播bean。
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
// 根据具体的上下文类来初始化不同的bean,比如当前为web工程,会创建themeSource, webServer(tomcat), 以及相关的bean。如:
// o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)...
onRefresh();
// Check for listener beans and register them.
// 重新将监听器注册到上边的事件广播bean中。
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
// 初始化剩下的一些bean,比如conversionService,是Spring Boot对beanFactory硬编码的一些配置。
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
// 清理掉上述过程创建和使用的缓存;
// 初始化LifecycleBean,并将各个Bean一一启动;
// 向所有监听器发布ContextRefreshedEvent事件。
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();
}
}
}
启动源码解析通读见这篇;
解析refreshContext见这篇。