SpringBoot啟動(dòng)應(yīng)用及回調(diào)監(jiān)聽原理解析
這篇文章主要介紹了SpringBoot啟動(dòng)應(yīng)用及回調(diào)監(jiān)聽原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
主類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文件中的類
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)類集合 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)境準(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)聽機(jī)制
- 詳解基于Spring Data的領(lǐng)域事件發(fā)布
- Springboot啟用多個(gè)監(jiān)聽端口代碼實(shí)例
- Spring Boot監(jiān)聽Redis Key失效事件實(shí)現(xiàn)定時(shí)任務(wù)的示例
- springboot+redis過期事件監(jiān)聽實(shí)現(xiàn)過程解析
- SpringBoot實(shí)現(xiàn)攔截器、過濾器、監(jiān)聽器過程解析
- SpringMVC事件監(jiān)聽ApplicationListener實(shí)例解析
- 詳解Spring事件發(fā)布與監(jiān)聽機(jī)制
相關(guān)文章
解決在啟動(dòng)eclipse的tomcat進(jìn)行訪問時(shí)出現(xiàn)404問題的方法
這篇文章主要介紹了解決在啟動(dòng)eclipse的tomcat進(jìn)行訪問時(shí)出現(xiàn)404問題的方法,感興趣的小伙伴們可以參考一下2016-04-04SpringBoot項(xiàng)目中使用Jsp的正確方法
SpringBoot默認(rèn)是不支持JSP開發(fā)的,若是需要使用JSP的話便需要自己配置外部的tomcat,下面這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目中使用Jsp的正確方法,需要的朋友可以參考下2023-05-05java 實(shí)現(xiàn)漢諾塔詳解及實(shí)現(xiàn)代碼
這篇文章主要介紹了java 實(shí)現(xiàn)漢諾塔詳解及實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04Java的Socket網(wǎng)絡(luò)編程基礎(chǔ)知識(shí)入門教程
這篇文章主要介紹了Java的Socket網(wǎng)絡(luò)編程基礎(chǔ)知識(shí)入門教程,包括基于TCP/IP和UDP協(xié)議的簡(jiǎn)單實(shí)例程序講解,需要的朋友可以參考下2016-01-01springboot啟動(dòng)時(shí)候報(bào)錯(cuò)mongodb問題
這篇文章主要介紹了springboot啟動(dòng)時(shí)候報(bào)錯(cuò)mongodb問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05在SpringBoot中如何利用Redis實(shí)現(xiàn)互斥鎖
當(dāng)我們利用Redis存儲(chǔ)熱點(diǎn)數(shù)據(jù)時(shí),突然就過期失效或者被刪除了,導(dǎo)致大量請(qǐng)求同時(shí)訪問數(shù)據(jù)庫,增加了數(shù)據(jù)庫的負(fù)載,為減輕數(shù)據(jù)庫的負(fù)載我們利用互斥鎖,本文重點(diǎn)介紹在SpringBoot中如何利用Redis實(shí)現(xiàn)互斥鎖,感興趣的朋友一起看看吧2023-09-09java實(shí)現(xiàn)把對(duì)象數(shù)組通過excel方式導(dǎo)出的功能
本文主要介紹了java實(shí)現(xiàn)把對(duì)象數(shù)組通過excel方式導(dǎo)出的功能的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-03-03