springboot启动源码分析1——初步初始化
springboot启动源码分析2——run方法分析
springboot启动源码分析3——环境配置
springboot启动源码分析4——刷新容器
springboot启动源码分析5——后续处理内容
Spring Boot启动源码分析6——refresh的invokeBeanFactoryPostProcessors方法详解
springboot启动源码分析7——自动配置
springboot启动源码分析8——aop
springboot源码分析5——后续处理内容
public ConfigurableApplicationContext run(String... args) {
long startTime = System.nanoTime();
DefaultBootstrapContext bootstrapContext = createBootstrapContext();
ConfigurableApplicationContext context = null;
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
context.setApplicationStartup(this.applicationStartup);
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
//刷新后处理,默认什么都不进行处理,等着你来重写重新实现处理的内容
afterRefresh(context, applicationArguments);
//这里记录程序运行时间,和上面刚开始的时候记录的时间相减。
Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
if (this.logStartupInfo) {
//创建启动日志
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);
}
//执行启动事件的监听器,一共有6个(我这里)
listeners.started(context, timeTakenToStartup);
//调用ApplicationContext和CommandLineRunner对应的运行的run方法
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
try {
//计算到进行ready的时间
Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
//执行监听器对应的springboot的application的准备类型的监听器。主要就是处理一些后续的工作,比如设置一些状态,发布一些对应的事件。
//包括你自定义的ListenerRun的run方法也是在这个时候执行的。
listeners.ready(context, timeTakenToReady);
}
catch (Throwable ex) {
handleRunFailure(context, ex, null);
throw new IllegalStateException(ex);
}
return context;
}
启动事件的监听器
完成之后返回上下文就初始化完毕了!