摘要:參考創(chuàng)建所有運(yùn)行監(jiān)聽(tīng)器并發(fā)布應(yīng)用啟動(dòng)事件來(lái)看下創(chuàng)建運(yùn)行監(jiān)聽(tīng)器相關(guān)的源碼創(chuàng)建邏輯和之前實(shí)例化初始化器和監(jiān)聽(tīng)器的一樣,一樣調(diào)用的是方法來(lái)獲取配置的監(jiān)聽(tīng)器名稱(chēng)并實(shí)例化所有的類(lèi)。
上篇《Spring Boot 2.x 啟動(dòng)全過(guò)程源碼分析(一)入口類(lèi)剖析》我們分析了 Spring Boot 入口類(lèi) SpringApplication 的源碼,并知道了其構(gòu)造原理,這篇我們繼續(xù)往下面分析其核心 run 方法。
[toc]
SpringApplication 實(shí)例 run 方法運(yùn)行過(guò)程上面分析了 SpringApplication 實(shí)例對(duì)象構(gòu)造方法初始化過(guò)程,下面繼續(xù)來(lái)看下這個(gè) SpringApplication 對(duì)象的 run 方法的源碼和運(yùn)行流程。
public ConfigurableApplicationContext run(String... args) { // 1、創(chuàng)建并啟動(dòng)計(jì)時(shí)監(jiān)控類(lèi) StopWatch stopWatch = new StopWatch(); stopWatch.start(); // 2、初始化應(yīng)用上下文和異常報(bào)告集合 ConfigurableApplicationContext context = null; CollectionexceptionReporters = new ArrayList<>(); // 3、設(shè)置系統(tǒng)屬性 `java.awt.headless` 的值,默認(rèn)值為:true configureHeadlessProperty(); // 4、創(chuàng)建所有 Spring 運(yùn)行監(jiān)聽(tīng)器并發(fā)布應(yīng)用啟動(dòng)事件 SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { // 5、初始化默認(rèn)應(yīng)用參數(shù)類(lèi) ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); // 6、根據(jù)運(yùn)行監(jiān)聽(tīng)器和應(yīng)用參數(shù)來(lái)準(zhǔn)備 Spring 環(huán)境 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); // 7、創(chuàng)建 Banner 打印類(lèi) Banner printedBanner = printBanner(environment); // 8、創(chuàng)建應(yīng)用上下文 context = createApplicationContext(); // 9、準(zhǔn)備異常報(bào)告器 exceptionReporters = getSpringFactoriesInstances( SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); // 10、準(zhǔn)備應(yīng)用上下文 prepareContext(context, environment, listeners, applicationArguments, printedBanner); // 11、刷新應(yīng)用上下文 refreshContext(context); // 12、應(yīng)用上下文刷新后置處理 afterRefresh(context, applicationArguments); // 13、停止計(jì)時(shí)監(jiān)控類(lèi) stopWatch.stop(); // 14、輸出日志記錄執(zhí)行主類(lèi)名、時(shí)間信息 if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); } // 15、發(fā)布應(yīng)用上下文啟動(dòng)完成事件 listeners.started(context); // 16、執(zhí)行所有 Runner 運(yùn)行器 callRunners(context, applicationArguments); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, listeners); throw new IllegalStateException(ex); } try { // 17、發(fā)布應(yīng)用上下文就緒事件 listeners.running(context); } catch (Throwable ex) { handleRunFailure(context, ex, exceptionReporters, null); throw new IllegalStateException(ex); } // 18、返回應(yīng)用上下文 return context; }
所以,我們可以按以下幾步來(lái)分解 run 方法的啟動(dòng)過(guò)程。
1、創(chuàng)建并啟動(dòng)計(jì)時(shí)監(jiān)控類(lèi)StopWatch stopWatch = new StopWatch(); stopWatch.start();
來(lái)看下這個(gè)計(jì)時(shí)監(jiān)控類(lèi) StopWatch 的相關(guān)源碼:
public void start() throws IllegalStateException { start(""); } public void start(String taskName) throws IllegalStateException { if (this.currentTaskName != null) { throw new IllegalStateException("Can"t start StopWatch: it"s already running"); } this.currentTaskName = taskName; this.startTimeMillis = System.currentTimeMillis(); }
首先記錄了當(dāng)前任務(wù)的名稱(chēng),默認(rèn)為空字符串,然后記錄當(dāng)前 Spring Boot 應(yīng)用啟動(dòng)的開(kāi)始時(shí)間。
2、初始化應(yīng)用上下文和異常報(bào)告集合ConfigurableApplicationContext context = null; Collection3、設(shè)置系統(tǒng)屬性 java.awt.headless 的值exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
設(shè)置該默認(rèn)值為:true,Java.awt.headless = true 有什么作用?
對(duì)于一個(gè) Java 服務(wù)器來(lái)說(shuō)經(jīng)常要處理一些圖形元素,例如地圖的創(chuàng)建或者圖形和圖表等。這些API基本上總是需要運(yùn)行一個(gè)X-server以便能使用AWT(Abstract Window Toolkit,抽象窗口工具集)。然而運(yùn)行一個(gè)不必要的 X-server 并不是一種好的管理方式。有時(shí)你甚至不能運(yùn)行 X-server,因此最好的方案是運(yùn)行 headless 服務(wù)器,來(lái)進(jìn)行簡(jiǎn)單的圖像處理。4、創(chuàng)建所有 Spring 運(yùn)行監(jiān)聽(tīng)器并發(fā)布應(yīng)用啟動(dòng)事件參考:www.cnblogs.com/princessd8251/p/4000016.html
SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting();
來(lái)看下創(chuàng)建 Spring 運(yùn)行監(jiān)聽(tīng)器相關(guān)的源碼:
private SpringApplicationRunListeners getRunListeners(String[] args) { Class>[] types = new Class>[] { SpringApplication.class, String[].class }; return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances( SpringApplicationRunListener.class, types, this, args)); } SpringApplicationRunListeners(Log log, Collection extends SpringApplicationRunListener> listeners) { this.log = log; this.listeners = new ArrayList<>(listeners); }
創(chuàng)建邏輯和之前實(shí)例化初始化器和監(jiān)聽(tīng)器的一樣,一樣調(diào)用的是 getSpringFactoriesInstances 方法來(lái)獲取配置的監(jiān)聽(tīng)器名稱(chēng)并實(shí)例化所有的類(lèi)。
SpringApplicationRunListener 所有監(jiān)聽(tīng)器配置在 spring-boot-2.0.3.RELEASE.jar!/META-INF/spring.factories 這個(gè)配置文件里面。
# Run Listeners org.springframework.boot.SpringApplicationRunListener= org.springframework.boot.context.event.EventPublishingRunListener5、初始化默認(rèn)應(yīng)用參數(shù)類(lèi)
ApplicationArguments applicationArguments = new DefaultApplicationArguments( args);6、根據(jù)運(yùn)行監(jiān)聽(tīng)器和應(yīng)用參數(shù)來(lái)準(zhǔn)備 Spring 環(huán)境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment);
下面我們主要來(lái)看下準(zhǔn)備環(huán)境的 prepareEnvironment 源碼:
private ConfigurableEnvironment prepareEnvironment( SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments) { // 6.1) 獲?。ɑ蛘邉?chuàng)建)應(yīng)用環(huán)境 ConfigurableEnvironment environment = getOrCreateEnvironment(); // 6.2) 配置應(yīng)用環(huán)境 configureEnvironment(environment, applicationArguments.getSourceArgs()); listeners.environmentPrepared(environment); bindToSpringApplication(environment); if (this.webApplicationType == WebApplicationType.NONE) { environment = new EnvironmentConverter(getClassLoader()) .convertToStandardEnvironmentIfNecessary(environment); } ConfigurationPropertySources.attach(environment); return environment; }
6.1) 獲?。ɑ蛘邉?chuàng)建)應(yīng)用環(huán)境
private ConfigurableEnvironment getOrCreateEnvironment() { if (this.environment != null) { return this.environment; } if (this.webApplicationType == WebApplicationType.SERVLET) { return new StandardServletEnvironment(); } return new StandardEnvironment(); }
這里分為標(biāo)準(zhǔn) Servlet 環(huán)境和標(biāo)準(zhǔn)環(huán)境。
6.2) 配置應(yīng)用環(huán)境
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) { configurePropertySources(environment, args); configureProfiles(environment, args); }
這里分為以下兩步來(lái)配置應(yīng)用環(huán)境。
配置 property sources
配置 Profiles
這里主要處理所有 property sources 配置和 profiles 配置。
7、創(chuàng)建 Banner 打印類(lèi)Banner printedBanner = printBanner(environment);
這是用來(lái)打印 Banner 的處理類(lèi),這個(gè)沒(méi)什么好說(shuō)的。
8、創(chuàng)建應(yīng)用上下文context = createApplicationContext();
來(lái)看下 createApplicationContext() 方法的源碼:
protected ConfigurableApplicationContext createApplicationContext() { Class> contextClass = this.applicationContextClass; if (contextClass == null) { try { switch (this.webApplicationType) { case SERVLET: contextClass = Class.forName(DEFAULT_WEB_CONTEXT_CLASS); break; case REACTIVE: contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS); break; default: contextClass = Class.forName(DEFAULT_CONTEXT_CLASS); } } catch (ClassNotFoundException ex) { throw new IllegalStateException( "Unable create a default ApplicationContext, " + "please specify an ApplicationContextClass", ex); } } return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass); }
其實(shí)就是根據(jù)不同的應(yīng)用類(lèi)型初始化不同的上下文應(yīng)用類(lèi)。
9、準(zhǔn)備異常報(bào)告器exceptionReporters = getSpringFactoriesInstances( SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context);
邏輯和之前實(shí)例化初始化器和監(jiān)聽(tīng)器的一樣,一樣調(diào)用的是 getSpringFactoriesInstances 方法來(lái)獲取配置的異常類(lèi)名稱(chēng)并實(shí)例化所有的異常處理類(lèi)。
該異常報(bào)告處理類(lèi)配置在 spring-boot-2.0.3.RELEASE.jar!/META-INF/spring.factories 這個(gè)配置文件里面。
# Error Reporters org.springframework.boot.SpringBootExceptionReporter= org.springframework.boot.diagnostics.FailureAnalyzers10、準(zhǔn)備應(yīng)用上下文
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
來(lái)看下 prepareContext() 方法的源碼:
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) { // 10.1)綁定環(huán)境到上下文 context.setEnvironment(environment); // 10.2)配置上下文的 bean 生成器及資源加載器 postProcessApplicationContext(context); // 10.3)為上下文應(yīng)用所有初始化器 applyInitializers(context); // 10.4)觸發(fā)所有 SpringApplicationRunListener 監(jiān)聽(tīng)器的 contextPrepared 事件方法 listeners.contextPrepared(context); // 10.5)記錄啟動(dòng)日志 if (this.logStartupInfo) { logStartupInfo(context.getParent() == null); logStartupProfileInfo(context); } // 10.6)注冊(cè)兩個(gè)特殊的單例bean context.getBeanFactory().registerSingleton("springApplicationArguments", applicationArguments); if (printedBanner != null) { context.getBeanFactory().registerSingleton("springBootBanner", printedBanner); } // 10.7)加載所有資源 Set11、刷新應(yīng)用上下文
refreshContext(context);
這個(gè)主要是刷新 Spring 的應(yīng)用上下文,源碼如下,不詳細(xì)說(shuō)明。
private void refreshContext(ConfigurableApplicationContext context) { refresh(context); if (this.registerShutdownHook) { try { context.registerShutdownHook(); } catch (AccessControlException ex) { // Not allowed in some environments. } } }12、應(yīng)用上下文刷新后置處理
afterRefresh(context, applicationArguments);
看了下這個(gè)方法的源碼是空的,目前可以做一些自定義的后置處理操作。
/** * Called after the context has been refreshed. * @param context the application context * @param args the application arguments */ protected void afterRefresh(ConfigurableApplicationContext context, ApplicationArguments args) { }13、停止計(jì)時(shí)監(jiān)控類(lèi)
stopWatch.stop();
public void stop() throws IllegalStateException { if (this.currentTaskName == null) { throw new IllegalStateException("Can"t stop StopWatch: it"s not running"); } long lastTime = System.currentTimeMillis() - this.startTimeMillis; this.totalTimeMillis += lastTime; this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime); if (this.keepTaskList) { this.taskList.add(this.lastTaskInfo); } ++this.taskCount; this.currentTaskName = null; }
計(jì)時(shí)監(jiān)聽(tīng)器停止,并統(tǒng)計(jì)一些任務(wù)執(zhí)行信息。
14、輸出日志記錄執(zhí)行主類(lèi)名、時(shí)間信息if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); }15、發(fā)布應(yīng)用上下文啟動(dòng)完成事件
listeners.started(context);
觸發(fā)所有 SpringApplicationRunListener 監(jiān)聽(tīng)器的 started 事件方法。
16、執(zhí)行所有 Runner 運(yùn)行器callRunners(context, applicationArguments);
private void callRunners(ApplicationContext context, ApplicationArguments args) { List
執(zhí)行所有 ApplicationRunner 和 CommandLineRunner 這兩種運(yùn)行器,不詳細(xì)展開(kāi)了。
17、發(fā)布應(yīng)用上下文就緒事件listeners.running(context);
觸發(fā)所有 SpringApplicationRunListener 監(jiān)聽(tīng)器的 running 事件方法。
18、返回應(yīng)用上下文return context;總結(jié)
Spring Boot 的啟動(dòng)全過(guò)程源碼分析至此,分析 Spring 源碼真是一個(gè)痛苦的過(guò)程,希望能給大家提供一點(diǎn)參考和思路,也希望能給正在 Spring Boot 學(xué)習(xí)路上的朋友一點(diǎn)收獲。
源碼分析不易,點(diǎn)贊 + 轉(zhuǎn)發(fā)支持一下吧!
推薦:Spring Boot & Cloud 最強(qiáng)技術(shù)教程
掃描關(guān)注我們的微信公眾號(hào),干貨每天更新。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/76716.html
摘要:核心注解講解最大的特點(diǎn)是無(wú)需配置文件,能自動(dòng)掃描包路徑裝載并注入對(duì)象,并能做到根據(jù)下的包自動(dòng)配置。所以最核心的個(gè)注解就是這是添加的一個(gè)注解,用來(lái)代替配置文件,所有這個(gè)配置文件里面能做到的事情都可以通過(guò)這個(gè)注解所在類(lèi)來(lái)進(jìn)行注冊(cè)。 最近面試一些 Java 開(kāi)發(fā)者,他們其中有些在公司實(shí)際用過(guò) Spring Boot, 有些是自己興趣愛(ài)好在業(yè)余自己學(xué)習(xí)過(guò)。然而,當(dāng)我問(wèn)他們 Spring Boo...
摘要:前言目前的大環(huán)境下,使用作為持久層框架還是占了絕大多數(shù)的,下面我們來(lái)說(shuō)一下使用的幾種姿勢(shì)。測(cè)試測(cè)試的程序與之前的一致,我們直接訪問(wèn),可以看到成功的結(jié)果姿勢(shì)三使用的姿勢(shì)和可以與上面兩種方式進(jìn)行結(jié)合,。。。接口的實(shí)現(xiàn)是通過(guò)。然后我們將的改為。 前言 目前的大環(huán)境下,使用Mybatis作為持久層框架還是占了絕大多數(shù)的,下面我們來(lái)說(shuō)一下使用Mybatis的幾種姿勢(shì)。 姿勢(shì)一:零配置注解開(kāi)發(fā) 第...
摘要:在項(xiàng)目中,為滿足以上要求,我們將大量的參數(shù)配置在或文件中,通過(guò)注解,我們可以方便的獲取這些參數(shù)值使用配置模塊假設(shè)我們正在搭建一個(gè)發(fā)送郵件的模塊。這使得在不影響其他模塊的情況下重構(gòu)一個(gè)模塊中的屬性變得容易。 在編寫(xiě)項(xiàng)目代碼時(shí),我們要求更靈活的配置,更好的模塊化整合。在 Spring Boot 項(xiàng)目中,為滿足以上要求,我們將大量的參數(shù)配置在 application.properties 或...
摘要:在項(xiàng)目中,為滿足以上要求,我們將大量的參數(shù)配置在或文件中,通過(guò)注解,我們可以方便的獲取這些參數(shù)值使用配置模塊假設(shè)我們正在搭建一個(gè)發(fā)送郵件的模塊。這使得在不影響其他模塊的情況下重構(gòu)一個(gè)模塊中的屬性變得容易。 在編寫(xiě)項(xiàng)目代碼時(shí),我們要求更靈活的配置,更好的模塊化整合。在 Spring Boot 項(xiàng)目中,為滿足以上要求,我們將大量的參數(shù)配置在 application.properties 或...
摘要:設(shè)置應(yīng)用上線文初始化器的作用是什么源碼如下。來(lái)看下方法源碼,其實(shí)就是初始化一個(gè)應(yīng)用上下文初始化器實(shí)例的集合。設(shè)置監(jiān)聽(tīng)器和設(shè)置初始化器調(diào)用的方法是一樣的,只是傳入的類(lèi)型不一樣,設(shè)置監(jiān)聽(tīng)器的接口類(lèi)型為,對(duì)應(yīng)的文件配置內(nèi)容請(qǐng)見(jiàn)下方。 Spring Boot 的應(yīng)用教程我們已經(jīng)分享過(guò)很多了,今天來(lái)通過(guò)源碼來(lái)分析下它的啟動(dòng)過(guò)程,探究下 Spring Boot 為什么這么簡(jiǎn)便的奧秘。 本篇基于 S...
閱讀 2706·2021-11-25 09:43
閱讀 2792·2021-11-04 16:09
閱讀 1785·2021-10-12 10:13
閱讀 933·2021-09-29 09:35
閱讀 938·2021-08-03 14:03
閱讀 1829·2019-08-30 15:55
閱讀 3063·2019-08-28 18:14
閱讀 3653·2019-08-26 13:43