SpringBoot啟動(dòng)應(yīng)用及回調(diào)監(jiān)聽(tīng)原理解析
這篇文章主要介紹了SpringBoot啟動(dòng)應(yīng)用及回調(diào)監(jiān)聽(tīng)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
主類(lèi)Main方法
public static void main(String[] args) {
SpringApplication.run(SpringBootRunApplication.class, args);
}
創(chuàng)建SpringApplication對(duì)象
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文件中的類(lèi)
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的實(shí)現(xiàn)類(lèi)集合
SpringApplicationRunListeners listeners = this.getRunListeners(args);
// 監(jiān)聽(tīng)開(kāi)始,回調(diào)所有SpringApplicationRunListener的starting()方法
listeners.starting();
Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 監(jiān)聽(tīng)配置環(huán)境準(zhǔ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);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解析Spring事件發(fā)布與監(jiān)聽(tīng)機(jī)制
- 詳解基于Spring Data的領(lǐng)域事件發(fā)布
- Springboot啟用多個(gè)監(jiān)聽(tīng)端口代碼實(shí)例
- Spring Boot監(jiān)聽(tīng)Redis Key失效事件實(shí)現(xiàn)定時(shí)任務(wù)的示例
- springboot+redis過(guò)期事件監(jiān)聽(tīng)實(shí)現(xiàn)過(guò)程解析
- SpringBoot實(shí)現(xiàn)攔截器、過(guò)濾器、監(jiān)聽(tīng)器過(guò)程解析
- SpringMVC事件監(jiān)聽(tīng)ApplicationListener實(shí)例解析
- 詳解Spring事件發(fā)布與監(jiān)聽(tīng)機(jī)制
相關(guān)文章
教你如何測(cè)試Spring Data JPA的Repository
Spring Data JPA 提供了一些便捷的方式來(lái)測(cè)試這種持久層的代碼,常見(jiàn)的兩種測(cè)試類(lèi)型是集成測(cè)試和單元測(cè)試,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08
Java開(kāi)發(fā)工具IntelliJ IDEA安裝圖解
這篇文章主要介紹了Java開(kāi)發(fā)工具IntelliJ IDEA安裝圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
java11新特性之集合轉(zhuǎn)換為數(shù)組的方法
Java11引入了一種將帶有泛型的集合轉(zhuǎn)換為帶有泛型的數(shù)組的簡(jiǎn)單方法,本文通過(guò)實(shí)例代碼介紹java11新特性之集合轉(zhuǎn)換為數(shù)組的操作方法,感興趣的朋友跟隨小編一起看看吧2024-06-06
java使用正則表達(dá)校驗(yàn)手機(jī)號(hào)碼示例(手機(jī)號(hào)碼正則)
這篇文章主要介紹了java使用正則表達(dá)校驗(yàn)手機(jī)號(hào)碼示例,可校驗(yàn)三個(gè)號(hào)碼段:13*、15*、18*,大家根據(jù)自己的需要增加自己的號(hào)碼段就可以了2014-03-03
Spring Boot集成Mybatis的實(shí)例代碼(簡(jiǎn)潔版)
這篇文章主要介紹了Spring Boot集成Mybatis簡(jiǎn)潔版的教程,需要的朋友可以參考下2018-02-02
使用Java快速將Web中表格轉(zhuǎn)換成Excel的方法
在平時(shí)做系統(tǒng)項(xiàng)目時(shí),經(jīng)常會(huì)需要做導(dǎo)出功能,下面這篇文章主要給大家介紹了關(guān)于使用Java快速將Web中表格轉(zhuǎn)換成Excel的相關(guān)資料,需要的朋友可以參考下2023-06-06

