您好,登錄后才能下訂單哦!
今天小編給大家分享一下SpringBoot執(zhí)行過程實(shí)例分析的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
每個(gè)Spring Boot項(xiàng)目都有一個(gè)主程序啟動類,在主程序啟動類中有一個(gè)啟動項(xiàng)目的main()方法,在該方法中通過執(zhí)行SpringApplication.run()即可啟動整個(gè)Spring Boot程序。
問題:那么SpringApplication.run()方法到底是如何做到啟動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()啟動項(xiàng)目,這兩個(gè)階段的實(shí)現(xiàn)具體說明如下
查看SpringApplication實(shí)例對象初始化創(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)目啟動類.class設(shè)置為屬性存儲起來 this.primarySources = new LinkedHashSet(Arrays.asList(primarySources)); // 判斷當(dāng)前webApplicationType應(yīng)用的類型 this.webApplicationType = WebApplicationType.deduceFromClasspath(); // 設(shè)置初始化器(Initializer),最后會調(diào)用這些初始化器 this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); // 設(shè)置監(jiān)聽器(Listener) this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class)); // 用于推斷并設(shè)置項(xiàng)目main()方法啟動的主程序啟動類 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è)置過程中,會使用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()方法啟動的主程序啟動類
分析完(new SpringApplication(primarySources)).run(args)源碼前一部分SpringApplication實(shí)例對象的初始化創(chuàng)建后,查看run(args)方法執(zhí)行的項(xiàng)目初始化啟動過程,核心代碼具體如下:
public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList(); this.configureHeadlessProperty(); // 第一步:獲取并啟動監(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打印器 - 就是啟動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)目初始化啟動過程大致包括以下部分:
第一步:獲取并啟動監(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)方法主要用于對項(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類型,會通過反射裝載對應(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)備動作。設(shè)置容器環(huán)境,包括各種變量等等,其中包含一個(gè)非常關(guān)鍵的操作:將啟動類注入容器,為后續(xù)開啟自動化配置奠定基礎(chǔ)
第五步:刷新容器
開啟刷新spring容器,通過refresh方法對整個(gè)IOC容器的初始化(包括bean資源的定位,解析,注冊等等),同時(shí)向JVM運(yùn)行時(shí)注冊一個(gè)關(guān)機(jī)鉤子,在JVM關(guān)機(jī)時(shí)會關(guān)閉這個(gè)上下文,除非當(dāng)時(shí)它已經(jīng)關(guān)閉
第六步:Spring容器后置處理
擴(kuò)展接口,設(shè)計(jì)模式中的模板方法,默認(rèn)為空實(shí)現(xiàn)。如果有自定義需求,可以重寫該方法。比如打印一些啟動結(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ā)布啟動事件。
第八步:執(zhí)行Runners
用于調(diào)用項(xiàng)目中自定義的執(zhí)行器XxxRunner類,使得在項(xiàng)目啟動完成后立即執(zhí)行一些特定程序。其中,Spring Boot提供的執(zhí)行器接口有ApplicationRunner 和CommandLineRunner兩種,在使用時(shí)只需要自定義一個(gè)執(zhí)行器類實(shí)現(xiàn)其中一個(gè)接口并重寫對應(yīng)的run()方法接口,然后Spring Boot項(xiàng)目啟動后會立即執(zhí)行這些特定程序
下面,通過一個(gè)Spring Boot執(zhí)行流程圖,讓大家更清晰的知道Spring Boot的整體執(zhí)行流程和主要啟動階段:
以上就是“SpringBoot執(zhí)行過程實(shí)例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。