}
//9.发布结束执行通知
listeners.started(context, timeTakenToStartup);
//10.执行 Runners
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
try {
Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() -
startTime);
//11.发布应用上下文就绪事件
listeners.ready(context, timeTakenToReady);
}
catch (Throwable ex) {
handleRunFailure(context, ex, null);
throw new IllegalStateException(ex);
}
return context;
}
发布 ApplicationReadyEvent 事件,启动完毕,表示服务已经可以开始正常提供服务了。
通常我们这里会监听这个事件来打印一些监控性质的日志,表示应用正常启动了。
SpringBoot 会触发其他的一些事件,这些事件按下列顺序触发:
(1)ApplicationStartingEvent:项目刚启动时触发,此时除了注册监听器和初始器之
外,其他所有处理都没有开始;
(2)ApplicationEnvironmentPreparedEvent:上下文得到环境信息之后触发,此时
上下文创建还没有创建;
(3)ApplicationPreparedEvent:bean 的定义信息加载完成之后触发,此时 bean 还
没有初始化;
(4)ApplicationReadyEvent:在所有 bean 初始化完毕,所有回调处理完成,系统准
备处理服务请求时触发;
(5)ApplicationFailedEvent:启动过程出现异常时候触发。
2. prepareContext()加载 bean 对象过程
BeanDe$nition(bean 对象)加载代码流程