上篇回顧
在上一篇refreshContext()刷新應(yīng)用上下文中, 我們主要分析了spring容器的刷新
- 首先更新刷新狀態(tài)
- 然后處理beanFactory的后置處理器, 用于注冊bean定義, 其中ConfigurationClassPostProcessor處理器, 處理模塊中@Configuration注解
- onRefresh()方法中, 實(shí)例化了TomcatWebServer
- 最后在finishRefresh()中, 實(shí)例化了所有bean
目錄
1. 啟動完成
2. 發(fā)布ApplicationStartedEvent事件
3. 發(fā)布ApplicationReadyEvent事件
4. 異常處理 handleRunFailure
1. 啟動完成
主要發(fā)布了ApplicationStartedEvent, ApplicationReadyEvent事件, 以及啟動失敗異常處理, 發(fā)布啟動失敗事件
public class SpringApplication {
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context);
//本文分析的是下面所有代碼
//沒有執(zhí)行任何操作
afterRefresh(context, applicationArguments);
//打印啟動時(shí)間日志
stopWatch.stop();
if (this.logStartupInfo) {
//打印啟動信息
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
//發(fā)布ApplicationStartedEvent事件
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
//異常處理
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
//發(fā)布ApplicationReadyEvent事件
listeners.running(context);
}
catch (Throwable ex) {
//異常處理
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
}
2. 發(fā)布ApplicationStartedEvent事件
有兩個(gè)listener關(guān)注了ApplicationStartedEvent事件,
- BackgroundPreinitializer
- 擴(kuò)展點(diǎn), 暫不做任何處理
- DelegatingApplicationListener
- 擴(kuò)展點(diǎn), 暫不做任何處理
3. 發(fā)布ApplicationReadyEvent事件
有三個(gè)監(jiān)聽器關(guān)注了ApplicationReadyEvent事件
- SpringApplicationAdminMXBeanRegistrar
- this.ready設(shè)為true
- BackgroundPreinitializer
- 等待或者終止后臺線程
- DelegatingApplicationListener
- 擴(kuò)展點(diǎn), 暫不做任何處理
4. 異常處理 handleRunFailure
public class SpringApplication {
//處理異常
private void handleRunFailure(ConfigurableApplicationContext context,
Throwable exception,
Collection<SpringBootExceptionReporter> exceptionReporters,
SpringApplicationRunListeners listeners) {
try {
try {
//獲取exitCode編碼
handleExitCode(context, exception);
//發(fā)布啟動失敗事件
if (listeners != null) {
listeners.failed(context, exception);
}
}
finally {
//上報(bào)失敗, 交給exceptionReporters處理
reportFailure(exceptionReporters, exception);
if (context != null) {
context.close();
}
}
}
catch (Exception ex) {
logger.warn("Unable to close ApplicationContext", ex);
}
//拋出異常
ReflectionUtils.rethrowRuntimeException(exception);
}
}
總結(jié)
到這一步, 整個(gè)springboot的啟動已經(jīng)結(jié)束, 最后發(fā)布了ApplicationStartedEvent, ApplicationReadyEvent事件, 以及啟動失敗異常處理, 發(fā)布啟動失敗事件