参照:https://blog.csdn.net/sessionsong/article/details/86682808
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.sources = new LinkedHashSet(); // 初始化
this.bannerMode = Mode.CONSOLE; // 控制台
this.logStartupInfo = true; // 打印log
this.addCommandLineProperties = true; //
this.addConversionService = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProfiles = new HashSet();
this.isCustomEnvironment = false;
this.resourceLoader = resourceLoader; // null值的loader
// primarySources是否为null
Assert.notNull(primarySources, "PrimarySources must not be null");
// 初始化并去重
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
// 类型 REACTIVE SERVLET NONE
this.webApplicationType = WebApplicationType.deduceFromClasspath();
// 初始化上下文
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
// 监听
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
// 主入口 一般是Class.forName(启动类)
this.mainApplicationClass = this.deduceMainApplicationClass();
}
// spring 秒表
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
this.configureHeadlessProperty();
// 创建并启动监听
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();
Collection exceptionReporters;
try {
// 初始化默认应用参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 根据监听准备spring环境
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
// 创建banner打印
Banner printedBanner = this.printBanner(environment);
// 创建上下文
context = this.createApplicationContext();
exceptionReporters =
// 异常报告器
this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// 刷新应用上下文
this.refreshContext(context);
// 应用上下文刷新后置处理
this.afterRefresh(context, applicationArguments);
// 停止秒表
stopWatch.stop();
if (this.logStartupInfo) {
// 输出日志
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
// 启动监听
listeners.started(context);
// 发布应用上下文启动完成
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
// 执行所有runner
listeners.running(context);
// 发布应用上下文就绪事件
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}