this.refreshContext(context);
当前context类型:
SpringApplication.refreshContext
可以看到,容器的刷新操作还是由容器自己实现。
- AnnotationConfigServletWebServerApplicationContext 没有自己的refresh操作,使用了父类的refresh(ServletWebServerApplicationContext)
ServletWebServerApplicationContext#refresh
public final void refresh() throws BeansException, IllegalStateException {
try {
super.refresh();
} catch (RuntimeException var3) {
WebServer webServer = this.webServer;
if (webServer != null) {
webServer.stop();
}
throw var3;
}
}
父类ServletWebServerApplicationContext在使用它的父类refresh的继承上的增加了一些东西。它的父类本来是GenericWebApplicationContext,但是它的父类使用的是顶层抽象类AbstractApplicationContext的refresh方法。
所以AnnotationConfigServletWebServerApplicationContext 用的是AbstractApplicationContext的refresh方法来刷新自己。
AbstractApplicationContext#refresh —— Spring启动过程中最重要的场景
public void refresh() throws BeansException, IllegalStateException {
synchronized(this.startupShutdownMonitor) {
StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
this.prepareRefresh();
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
this.prepareBeanFactory(beanFactory);
try {
this.postProcessBeanFactory(beanFactory);
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
this.invokeBeanFactoryPostProcessors(beanFactory);
this.registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
this.initMessageSource();
this.initApplicationEventMulticaster();
this.onRefresh();
this.registerListeners();
this.finishBeanFactoryInitialization(beanFactory);
this.finishRefresh();
} catch (BeansException var10) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var10);
}
this.destroyBeans();
this.cancelRefresh(var10);
throw var10;
} finally {
this.resetCommonCaches();
contextRefresh.end();
}
}
}