SpringBoot啟動應(yīng)用及回調(diào)監(jiān)聽原理解析
這篇文章主要介紹了SpringBoot啟動應(yīng)用及回調(diào)監(jiān)聽原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
主類Main方法
public static void main(String[] args) {
SpringApplication.run(SpringBootRunApplication.class, args);
}
創(chuàng)建SpringApplication對象
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return (new SpringApplication(primarySources)).run(args);
}
構(gòu)造器
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));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = this.deduceMainApplicationClass();
}
ApplicationContextInitializer&ApplicationListener
讀取META-INF/spring.factories文件中的類
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
執(zhí)行run方法
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// IOC容器
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
this.configureHeadlessProperty();
// 讀取META-INF/spring.factories文件獲得SpringApplicationRunListener的實現(xiàn)類集合
SpringApplicationRunListeners listeners = this.getRunListeners(args);
// 監(jiān)聽開始,回調(diào)所有SpringApplicationRunListener的starting()方法
listeners.starting();
Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 監(jiān)聽配置環(huán)境準備好了,回調(diào)所有SpringApplicationRunListener的environmentPrepared()方法
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
// 創(chuàng)建IOC容器
context = this.createApplicationContext();
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
// 回調(diào)ApplicationContextInitializer的initialize()方法,回調(diào)SpringApplicationRunListener的contextPrepared()方法
// 回調(diào)SpringApplicationRunListener的contextLoaded()方法
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// 刷新IOC容器,注入組件
this.refreshContext(context);
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
// 回調(diào)SpringApplicationRunListener的started()方法
listeners.started(context);
// 從IOC容器中獲得ApplicationRunner、CommandLineRunner的所有組件,回調(diào)ApplicationRunner、CommandLineRunner的run()方法
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
// 回調(diào)SpringApplicationRunListener的running()方法
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
教你如何測試Spring Data JPA的Repository
Spring Data JPA 提供了一些便捷的方式來測試這種持久層的代碼,常見的兩種測試類型是集成測試和單元測試,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-08-08
java11新特性之集合轉(zhuǎn)換為數(shù)組的方法
Java11引入了一種將帶有泛型的集合轉(zhuǎn)換為帶有泛型的數(shù)組的簡單方法,本文通過實例代碼介紹java11新特性之集合轉(zhuǎn)換為數(shù)組的操作方法,感興趣的朋友跟隨小編一起看看吧2024-06-06
Spring Boot集成Mybatis的實例代碼(簡潔版)
這篇文章主要介紹了Spring Boot集成Mybatis簡潔版的教程,需要的朋友可以參考下2018-02-02
使用Java快速將Web中表格轉(zhuǎn)換成Excel的方法
在平時做系統(tǒng)項目時,經(jīng)常會需要做導(dǎo)出功能,下面這篇文章主要給大家介紹了關(guān)于使用Java快速將Web中表格轉(zhuǎn)換成Excel的相關(guān)資料,需要的朋友可以參考下2023-06-06

