SpringBoot RUN方法執行流程
1、查看main方法
@SpringBootApplication // 能夠掃描Spring組件並自動配置Spring Boot
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
2、點進run方法
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) { // 調用重載方法
return run(new Class[]{primarySource}, args);
}
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { // 兩件事:1.初始化SpringApplication 2.執行run方法
return (new SpringApplication(primarySources)).run(args);
}
- 初始化SpringApplication對象
- 執行run方法
3、初始化SpringApplication對象
public SpringApplication(Class<?>... primarySources) {
this((ResourceLoader)null, primarySources);
}
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 = Collections.emptySet();
this.isCustomEnvironment = false;
this.lazyInitialization = false;
this.applicationContextFactory = ApplicationContextFactory.DEFAULT;
this.applicationStartup = ApplicationStartup.DEFAULT;
// 設置資源加載器
this.resourceLoader = resourceLoader;
// 斷言加載資源類不能為null
Assert.notNull(primarySources, "PrimarySources must not be null");
// 將primarySources數組轉換為List,最后放到LinkedHashSet集合中
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
//【1.1 推斷應用類型,后面會根據類型初始化對應的環境。常用的一般都是servlet環境 】
this.webApplicationType = WebApplicationType.deduceFromClasspath();
this.bootstrapRegistryInitializers = this.getBootstrapRegistryInitializersFromSpringFactories();
// 【1.2 初始化classpath下 META-INF/spring.factories中已配置的 ApplicationContextInitializer 】
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
// 【1.3 初始化classpath下所有已配置的 ApplicationListener 】
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
// 【1.4 根據調用棧,推斷出 main 方法的類名 】
this.mainApplicationClass = this.deduceMainApplicationClass();
}
4、 run(args)源碼剖析
public ConfigurableApplicationContext run(String... args) {
//記錄程序運行時間
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// 注解bootstrap 初始化器到BootStrapContext上下文中
DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
ConfigurableApplicationContext context = null;
// 設置 java.awt.headless屬性
this.configureHeadlessProperty();
//從META-INF/spring.factories中獲取監聽器 `SpringApplicationRunListener`
//1、獲取並啟動監聽器
SpringApplicationRunListeners listeners = this.getRunListeners(args);
// 循環遍歷啟動監聽器
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
// 封裝參數對象
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//2、構造應用上下文環境,看構建的是SERVLET、REACTIVE、還是普通環境
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
//處理需要忽略的Bean
this.configureIgnoreBeanInfo(environment);
//打印banner
Banner printedBanner = this.printBanner(environment);
//3、初始化應用上下文
context = this.createApplicationContext();
context.setApplicationStartup(this.applicationStartup);
//4、刷新應用上下文前的准備階段
this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
//5、刷新應用上下文
this.refreshContext(context);
//刷新應用上下文后的擴展接口
this.afterRefresh(context, applicationArguments);
//時間記錄停止
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
//發布容器啟動完成事件
listeners.started(context);
// 將ApplicationRunner、CommandLineRunner注入到list里面並排序,然后循環調用Runner的run方法
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, listeners);
throw new IllegalStateException(var10);
}
try {
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
以后就是主要分六步
-
第1步:創建並啟動監聽器listener
-
第2步:構建上下文環境,看是創建看構建的是SERVLET、REACTIVE、還是普通環境ApplicationEnvironment
-
第3步:初始化應用上下文
-
第4步:刷新應用上下文前的准備階段,
-
第5步:刷新上下文並且注冊了一個鈎子(JVM關閉就會關閉這個上下文)
-
第6步:刷新應用上下文后的擴展接口,afterRefresh這個是一個空實現的方法
