1.创建监控器(监控运行时间)StopWatch stopWatch = new StopWatch();
2.创建空的IOC容器,和一组异常报告器
3.配置awt,图标配置
4.创建运行侦听器(SpringApplicationRunListeners listeners = this.getRunListeners(args);)
5.准备运行环境(DefaultApplicationArguments,默认应用程序参数)
6.配置系统参数(this.configureIgnoreBeanInfo(environment),environment,默认环境)
7.打印图标Banner(this.printBanner(environment);)
8.创建IOC容器映射(ApplicationContext)
9.初始化IOC容器(this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);)
10.刷新IOC容器(this.refreshContext(context);)
11.刷新最后的处理(this.afterRefresh(context, applicationArguments);) ,停止监听器(stopWatch.stop();)
12.发布运行侦听器 (this.callRunners(context, applicationArguments);)
13.运行器回调(打印信息,提示用户spring已经启动成功),spring启动完成
public ConfigurableApplicationContext run(String... args) {
//1.创建时间性能监控器
StopWatch stopWatch = new StopWatch();
//启动监听器
stopWatch.start();
//2.创建内容为null的IOC容器
ConfigurableApplicationContext context = null;
//创建异常报告
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
//3.配置awt(图标)的相关信息
this.configureHeadlessProperty();
//3.创建运行侦听器
SpringApplicationRunListeners listeners = this.getRunListeners(args);
//启动侦听器
listeners.starting();
Collection exceptionReporters;
try {
//5.准备运行时环境
ApplicationArguments applicationArguments =
new DefaultApplicationArguments(args);
ConfigurableEnvironment environment =
this.prepareEnvironment(listeners, applicationArguments);
//6.配置系统参数
this.configureIgnoreBeanInfo(environment);
//7.打印图标
Banner printedBanner = this.printBanner(environment);
//8.创建IOC容器
context = this.createApplicationContext();
//设置异常报告
exceptionReporters = this.getSpringFactoriesInstances(
SpringBootExceptionReporter.class, new Class[]{
ConfigurableApplicationContext.class}, context);
//9.初始化IOC容器
this.prepareContext(context, environment, listeners,
applicationArguments, printedBanner);
//10.刷新IOC容器
this.refreshContext(context);
//11.刷新后的处理
this.afterRefresh(context, applicationArguments);
//监听器停止
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass))
.logStarted(this.getApplicationLog(), stopWatch);
}
//12.发布started事件
listeners.started(context);
//13.运行器回调
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters,
(SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
//3.配置awt的相关信息
private void configureHeadlessProperty() {
System.setProperty("java.awt.headless", System.getProperty(
"java.awt.headless", Boolean.toString(this.headless)));
}
//4.配置运行侦听器
private SpringApplicationRunListeners getRunListeners(String[] args) {
Class<?>[] types = new Class[]{SpringApplication.class, String[].class};
return new SpringApplicationRunListeners(logger,
this.getSpringFactoriesInstances(SpringApplicationRunListener.class,
types, this, args));
}