启动分为两部分,先创建Application然后调用实例的run方法。
1、new Application执行流程
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.sources = new LinkedHashSet();
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
this.addCommandLineProperties = true;
this.addConversionService = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProfiles = new HashSet();
this.isCustomEnvironment = false;
this.lazyInitialization = false;
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
//保存主配置类
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
//判断应用是否是web应用环境
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//从类路径下META-INF的spring.factoris文件里找到所有的ApplicationContextInitializer,然后保存起来
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
//从类路径下的META-INF下的spring.factories下找到所有的ApplicationListener类,保存起来
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
//从几个配置类当中找到主配置类
this.mainApplicationClass = this.deduceMainApplicationClass();
}
2.SpringApplication.run(args)流程
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
this.configureHeadlessProperty();
//获取SpringApplicationRunListeners,从类路径META-INF的spring.factories找所有的SpringApplicationRunListener
SpringApplicationRunListeners listeners = this.getRunListeners(args);
//遍历所有的SpringApplicationRunListener的start()方法
listeners.starting();
Collection exceptionReporters;
try {
//封装命令行参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//准备环境,创建完环境之后调用SpringApplicationRunListener的environmentPrepared方法,表示环境准备完成
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
//控制台打印banner图标
Banner printedBanner = this.printBanner(environment);
//创建ApplicationContext,决定创建web的ioc容器还是普通的ioc容器
context = this.createApplicationContext();
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
//准备上下文环境 将environment保存到ioc容器当中,而且applyInitializers();
//applyInitializers() 调用之前加载所有的ApplicationContextInitializer的initialize()方法
//同时调用之前加载所有的SpringApplicationRunListener的contextPrepared()方法
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//prepareContext运行完成时 再调用所有的SpringApplicationRunListeners的contextLoaded()方法
//重构ioc容器,就是ioc容器的初始化(如果是web应用,还会创建嵌入式的tomcat)
this.refreshContext(context);
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
//回调所有的SpringApplicationRunListeners的start()方法
listeners.started(context);
//调用ApplicationRunner和CommandLineRunner的方法
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
//调用所有的SpringApplicationRunListeners的running()方法
listeners.running(context);
//整个springboot应用启动以后返回启动的ioc容器
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}