SpringBoot 的 run方法

SpringBoot 的run方法執(zhí)行,內(nèi)部執(zhí)行并未去徹底了解到,可能是自我的基礎(chǔ)未達(dá)到,很多思想都沒能去理解(大概也看有一周的時間了(當(dāng)然也不是每天一直看,一天大概回去看一兩個小時)),純屬小白,這篇文章純屬筆記。

會先在堆中new 一個SpringApplication對象(并調(diào)用構(gòu)造器為SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) ) 這里注意: SpringApplication中有部分靜態(tài)屬性、還有一些默認(rèn)屬性回去加載;

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources){ this.resourceLoader = resourceLoader; //資源加載器 默認(rèn)傳參數(shù)為null Assert.notNull(primarySources, "PrimarySources must not be null"); // 用來判斷傳入的class是否為null、null則拋出String信息

this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources)); // 參數(shù)數(shù)組轉(zhuǎn)換為集合

this.webApplicationType = WebApplicationType.deduceFromClasspath(); // 映射以公共Java語言類名為鍵,對應(yīng)的類為值。

setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class)); // 設(shè)置初始化類集合(spring工廠上下文)

setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); // 設(shè)置初始化監(jiān)聽對象(spring工廠)

this.mainApplicationClass = deduceMainApplicationClass(); // 返回堆棧中的main方法所在的class文件

}

// 初始話結(jié)束調(diào)用run()方法

public ConfigurableApplicationContext run(String... args) {

StopWatch stopWatch = new StopWatch(); //springboot中的計時對象 stopWatch.start(); // 計時開始

ConfigurableApplicationContext context = null; // 初始化配置對象 用于返回 Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();// 初始化springboot異常記錄對象集合

configureHeadlessProperty();// 配置其它屬性

SpringApplicationRunListeners listeners = getRunListeners(args); // 配置運行時監(jiān)聽對象(屬性有日志對象,監(jiān)聽對象集合)

listeners.starting(); // 遍歷集合對象調(diào)用每個對象相應(yīng)的實現(xiàn)

try {

ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);// 設(shè)置應(yīng)用參數(shù)

ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); // 準(zhǔn)備環(huán)境(創(chuàng)建標(biāo)準(zhǔn)的服務(wù)環(huán)境StandardServletEnvironment[里面的屬性都是系統(tǒng)默認(rèn)的]){ 獲取配置環(huán)境對象;配置環(huán)境對象;spring應(yīng)用運行監(jiān)聽對象會為該環(huán)境對象做一些準(zhǔn)備工作;把該環(huán)境對象賦值給綁定對象的容器中; 判斷是否是定制的環(huán)境,不是則創(chuàng)建環(huán)境轉(zhuǎn)換器去調(diào)用convertEnvironmentIfNecessary(environment,deduceEnvironmentClass())進行轉(zhuǎn)換; 附加到環(huán)境屬性源上 }

configureIgnoreBeanInfo(environment); // 配置忽略對象信息

Banner printedBanner = printBanner(environment);

context = createApplicationContext(); // 創(chuàng)建應(yīng)用上下文(通過獲取上下文的class去創(chuàng)建對象)

exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); // 獲取spring工廠的示例(通過獲取工廠集合中的class去一一創(chuàng) 建對象返回相應(yīng)的對象集合)

prepareContext(context, environment, listeners, applicationArguments, printedBanner); // 準(zhǔn)備上下文 { 設(shè)置上下文的環(huán)境;上下文對象配置;初始化上下文;監(jiān)聽者上下文準(zhǔn)備(遍歷監(jiān)聽者集合為每個監(jiān)聽者調(diào)用各自的contextPrepared()方法); 啟動日志信息; } refreshContext(context); // 刷新上下文對象

afterRefresh(context, applicationArguments); // 之后刷新

stopWatch.stop(); // 計時結(jié)束

if (this.logStartupInfo) {

new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); // 開啟一個日志對象

}

listeners.started(context); // 監(jiān)聽者開啟上下文(遍歷監(jiān)聽者集合為每個監(jiān)聽者調(diào)用各自的started()方法)

callRunners(context, applicationArguments);

} catch (Throwable ex) {

handleRunFailure(context, ex, exceptionReporters, listeners);

throw new IllegalStateException(ex);

}

try { listeners.running(context); }

catch (Throwable ex) {

handleRunFailure(context, ex, exceptionReporters, null);

throw new IllegalStateException(ex);

}

return context;

}

暫未整理完;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容