上篇回顾
在上一篇refreshContext()刷新应用上下文中, 我们主要分析了spring容器的刷新
- 首先更新刷新状态
- 然后处理beanFactory的后置处理器, 用于注册bean定义, 其中ConfigurationClassPostProcessor处理器, 处理模块中@Configuration注解
- onRefresh()方法中, 实例化了TomcatWebServer
- 最后在finishRefresh()中, 实例化了所有bean
目录
1. 启动完成
2. 发布ApplicationStartedEvent事件
3. 发布ApplicationReadyEvent事件
4. 异常处理 handleRunFailure
1. 启动完成
主要发布了ApplicationStartedEvent, ApplicationReadyEvent事件, 以及启动失败异常处理, 发布启动失败事件
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);
//本文分析的是下面所有代码
//没有执行任何操作
afterRefresh(context, applicationArguments);
//打印启动时间日志
stopWatch.stop();
if (this.logStartupInfo) {
//打印启动信息
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
//发布ApplicationStartedEvent事件
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
//异常处理
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
//发布ApplicationReadyEvent事件
listeners.running(context);
}
catch (Throwable ex) {
//异常处理
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
}
2. 发布ApplicationStartedEvent事件
有两个listener关注了ApplicationStartedEvent事件,
- BackgroundPreinitializer
- 扩展点, 暂不做任何处理
- DelegatingApplicationListener
- 扩展点, 暂不做任何处理
3. 发布ApplicationReadyEvent事件
有三个监听器关注了ApplicationReadyEvent事件
- SpringApplicationAdminMXBeanRegistrar
- this.ready设为true
- BackgroundPreinitializer
- 等待或者终止后台线程
- DelegatingApplicationListener
- 扩展点, 暂不做任何处理
4. 异常处理 handleRunFailure
public class SpringApplication {
//处理异常
private void handleRunFailure(ConfigurableApplicationContext context,
Throwable exception,
Collection<SpringBootExceptionReporter> exceptionReporters,
SpringApplicationRunListeners listeners) {
try {
try {
//获取exitCode编码
handleExitCode(context, exception);
//发布启动失败事件
if (listeners != null) {
listeners.failed(context, exception);
}
}
finally {
//上报失败, 交给exceptionReporters处理
reportFailure(exceptionReporters, exception);
if (context != null) {
context.close();
}
}
}
catch (Exception ex) {
logger.warn("Unable to close ApplicationContext", ex);
}
//抛出异常
ReflectionUtils.rethrowRuntimeException(exception);
}
}
总结
到这一步, 整个springboot的启动已经结束, 最后发布了ApplicationStartedEvent, ApplicationReadyEvent事件, 以及启动失败异常处理, 发布启动失败事件