SpringBoot詳解執(zhí)行過程
每個(gè)Spring Boot項(xiàng)目都有一個(gè)主程序啟動(dòng)類,在主程序啟動(dòng)類中有一個(gè)啟動(dòng)項(xiàng)目的main()方法,在該方法中通過執(zhí)行SpringApplication.run()即可啟動(dòng)整個(gè)Spring Boot程序。
問題:那么SpringApplication.run()方法到底是如何做到啟動(dòng)Spring Boot項(xiàng)目的呢?
下面我們查看run()方法內(nèi)部的源碼,核心代碼具體如下:
@SpringBootApplication public class SpringbootDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootDemoApplication.class, args); } }
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) { return run(new Class[]{primarySource}, args); } public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { return (new SpringApplication(primarySources)).run(args); }
從上述源碼可以看出,SpringApplication.run()方法內(nèi)部執(zhí)行了兩個(gè)操作,分別是SpringApplication實(shí)例的初始化創(chuàng)建和調(diào)用run()啟動(dòng)項(xiàng)目,這兩個(gè)階段的實(shí)現(xiàn)具體說明如下
1.SpringApplication實(shí)例的初始化創(chuàng)建
查看SpringApplication實(shí)例對(duì)象初始化創(chuàng)建的源碼信息,核心代碼具體如下
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.resourceLoader = resourceLoader; Assert.notNull(primarySources, "PrimarySources must not be null"); // 把項(xiàng)目啟動(dòng)類.class設(shè)置為屬性存儲(chǔ)起來 this.primarySources = new LinkedHashSet(Arrays.asList(primarySources)); // 判斷當(dāng)前webApplicationType應(yīng)用的類型 this.webApplicationType = WebApplicationType.deduceFromClasspath(); // 設(shè)置初始化器(Initializer),最后會(huì)調(diào)用這些初始化器 this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); // 設(shè)置監(jiān)聽器(Listener) this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class)); // 用于推斷并設(shè)置項(xiàng)目main()方法啟動(dòng)的主程序啟動(dòng)類 this.mainApplicationClass = this.deduceMainApplicationClass();
從上述源碼可以看出,SpringApplication的初始化過程主要包括4部分,具體說明如下。
(1)this.webApplicationType = WebApplicationType.deduceFromClasspath()
用于判斷當(dāng)前webApplicationType應(yīng)用的類型。deduceFromClasspath()方法用于查看Classpath類路徑下是否存在某個(gè)特征類,從而判斷當(dāng)前webApplicationType類型是SERVLET應(yīng)用(Spring 5之前的傳統(tǒng)MVC應(yīng)用)還是REACTIVE應(yīng)用(Spring 5開始出現(xiàn)的WebFlux交互式應(yīng)用)
(2)this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class))
用于SpringApplication應(yīng)用的初始化器設(shè)置。在初始化器設(shè)置過程中,會(huì)使用Spring類加載器SpringFactoriesLoader從META-INF/spring.factories類路徑下的META-INF下的spring.factores文件中獲取所有可用的應(yīng)用初始化器類ApplicationContextInitializer。
(3)this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class))
用于SpringApplication應(yīng)用的監(jiān)聽器設(shè)置。監(jiān)聽器設(shè)置的過程與上一步初始化器設(shè)置的過程基本一樣,也是使用SpringFactoriesLoader從META-INF/spring.factories類路徑下的META-INF下的spring.factores文件中獲取所有可用的監(jiān)聽器類ApplicationListener。
(4)this.mainApplicationClass = this.deduceMainApplicationClass()
用于推斷并設(shè)置項(xiàng)目main()方法啟動(dòng)的主程序啟動(dòng)類
2.項(xiàng)目的初始化啟動(dòng)
分析完(new SpringApplication(primarySources)).run(args)源碼前一部分SpringApplication實(shí)例對(duì)象的初始化創(chuàng)建后,查看run(args)方法執(zhí)行的項(xiàng)目初始化啟動(dòng)過程,核心代碼具體如下:
public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList(); this.configureHeadlessProperty(); // 第一步:獲取并啟動(dòng)監(jiān)聽器 SpringApplicationRunListeners listeners = this.getRunListeners(args); listeners.starting(); Collection exceptionReporters; try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); // 第二步:根據(jù)SpringApplicationRunListeners以及參數(shù)來準(zhǔn)備環(huán)境 ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments); this.configureIgnoreBeanInfo(environment); // 準(zhǔn)備Banner打印器 - 就是啟動(dòng)Spring Boot的時(shí)候打印在console上的ASCII藝術(shù)字體 Banner printedBanner = this.printBanner(environment); // 第三步:創(chuàng)建Spring容器 context = this.createApplicationContext(); exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, new Object[]{context}); // 第四步:Spring容器前置處理 this.prepareContext(context, environment, listeners, applicationArguments, printedBanner); // 第五步:刷新容器 this.refreshContext(context); // 第六步:Spring容器后置處理 this.afterRefresh(context, applicationArguments); stopWatch.stop(); if (this.logStartupInfo) { (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch); } // 第七步:發(fā)出結(jié)束執(zhí)行的事件 listeners.started(context); // 返回容器 this.callRunners(context, applicationArguments); } catch (Throwable var10) { this.handleRunFailure(context, var10, exceptionReporters, listeners); throw new IllegalStateException(var10); } try { listeners.running(context); return context; } catch (Throwable var9) { this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners) null); throw new IllegalStateException(var9); } }
從上述源碼可以看出,項(xiàng)目初始化啟動(dòng)過程大致包括以下部分:
第一步:獲取并啟動(dòng)監(jiān)聽器
this.getRunListeners(args)和listeners.starting()方法主要用于獲取SpringApplication實(shí)例初始化過程中初始化的SpringApplicationRunListener監(jiān)聽器并運(yùn)行。
第二步:根據(jù)SpringApplicationRunListeners以及參數(shù)來準(zhǔn)備環(huán)境
this.prepareEnvironment(listeners, applicationArguments)方法主要用于對(duì)項(xiàng)目運(yùn)行環(huán)境進(jìn)行預(yù)設(shè)置,同時(shí)通過this.configureIgnoreBeanInfo(environment)方法排除一些不需要的運(yùn)行環(huán)境
第三步:創(chuàng)建Spring容器
根據(jù)webApplicationType進(jìn)行判斷, 確定容器類型,如果該類型為SERVLET類型,會(huì)通過反射裝載對(duì)應(yīng)的字節(jié)碼,也就是AnnotationConfigServletWebServerApplicationContext,接著使用之前初始化設(shè)置的context(應(yīng)用上下文環(huán)境)、environment(項(xiàng)目運(yùn)行環(huán)境)、listeners(運(yùn)行監(jiān)聽器)、applicationArguments(項(xiàng)目參數(shù))和printedBanner(項(xiàng)目圖標(biāo)信息)進(jìn)行應(yīng)用上下文的組裝配置,并刷新配置
第四步:Spring容器前置處理
這一步主要是在容器刷新之前的準(zhǔn)備動(dòng)作。設(shè)置容器環(huán)境,包括各種變量等等,其中包含一個(gè)非常關(guān)鍵的操作:將啟動(dòng)類注入容器,為后續(xù)開啟自動(dòng)化配置奠定基礎(chǔ)
第五步:刷新容器
開啟刷新spring容器,通過refresh方法對(duì)整個(gè)IOC容器的初始化(包括bean資源的定位,解析,注冊(cè)等等),同時(shí)向JVM運(yùn)行時(shí)注冊(cè)一個(gè)關(guān)機(jī)鉤子,在JVM關(guān)機(jī)時(shí)會(huì)關(guān)閉這個(gè)上下文,除非當(dāng)時(shí)它已經(jīng)關(guān)閉
第六步:Spring容器后置處理
擴(kuò)展接口,設(shè)計(jì)模式中的模板方法,默認(rèn)為空實(shí)現(xiàn)。如果有自定義需求,可以重寫該方法。比如打印一些啟動(dòng)結(jié)束log,或者一些其它后置處理。
第七步:發(fā)出結(jié)束執(zhí)行的事件
獲取EventPublishingRunListener監(jiān)聽器,并執(zhí)行其started方法,并且將創(chuàng)建的Spring容器傳進(jìn)去了,創(chuàng)建一個(gè)ApplicationStartedEvent事件,并執(zhí)行ConfigurableApplicationContext 的
publishEvent方法,也就是說這里是在Spring容器中發(fā)布事件,并不是在SpringApplication中發(fā)布事件,和前面的starting是不同的,前面的starting是直接向SpringApplication中的監(jiān)聽器發(fā)布啟動(dòng)事件。
第八步:執(zhí)行Runners
用于調(diào)用項(xiàng)目中自定義的執(zhí)行器XxxRunner類,使得在項(xiàng)目啟動(dòng)完成后立即執(zhí)行一些特定程序。其中,Spring Boot提供的執(zhí)行器接口有ApplicationRunner 和CommandLineRunner兩種,在使用時(shí)只需要自定義一個(gè)執(zhí)行器類實(shí)現(xiàn)其中一個(gè)接口并重寫對(duì)應(yīng)的run()方法接口,然后Spring Boot項(xiàng)目啟動(dòng)后會(huì)立即執(zhí)行這些特定程序
下面,通過一個(gè)Spring Boot執(zhí)行流程圖,讓大家更清晰的知道Spring Boot的整體執(zhí)行流程和主要啟動(dòng)階段:
到此這篇關(guān)于SpringBoot詳解執(zhí)行過程的文章就介紹到這了,更多相關(guān)SpringBoot執(zhí)行過程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot運(yùn)行到dokcer中 dockerfile的場(chǎng)景分析
這篇文章主要介紹了springboot運(yùn)行到dokcer中 dockerfile,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03Request的包裝類HttpServletRequestWrapper的使用說明
這篇文章主要介紹了Request的包裝類HttpServletRequestWrapper的使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08