SpringBoot 的run方法執行,內部執行并未去徹底了解到,可能是自我的基礎未達到,很多思想都沒能去理解(大概也看有一周的時間了(當然也不是每天一直看,一天大概回去看一兩個小時)),純屬小白,這篇文章純屬筆記。
會先在堆中new 一個SpringApplication對象(并調用構造器為SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) ) 這里注意: SpringApplication中有部分靜態屬性、還有一些默認屬性回去加載;
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources){ this.resourceLoader = resourceLoader; //資源加載器 默認傳參數為null Assert.notNull(primarySources, "PrimarySources must not be null"); // 用來判斷傳入的class是否為null、null則拋出String信息
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources)); // 參數數組轉換為集合
this.webApplicationType = WebApplicationType.deduceFromClasspath(); // 映射以公共Java語言類名為鍵,對應的類為值。
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class)); // 設置初始化類集合(spring工廠上下文)
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); // 設置初始化監聽對象(spring工廠)
this.mainApplicationClass = deduceMainApplicationClass(); // 返回堆棧中的main方法所在的class文件
}
// 初始話結束調用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); // 配置運行時監聽對象(屬性有日志對象,監聽對象集合)
listeners.starting(); // 遍歷集合對象調用每個對象相應的實現
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);// 設置應用參數
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); // 準備環境(創建標準的服務環境StandardServletEnvironment[里面的屬性都是系統默認的]){ 獲取配置環境對象;配置環境對象;spring應用運行監聽對象會為該環境對象做一些準備工作;把該環境對象賦值給綁定對象的容器中; 判斷是否是定制的環境,不是則創建環境轉換器去調用convertEnvironmentIfNecessary(environment,deduceEnvironmentClass())進行轉換; 附加到環境屬性源上 }
configureIgnoreBeanInfo(environment); // 配置忽略對象信息
Banner printedBanner = printBanner(environment);
context = createApplicationContext(); // 創建應用上下文(通過獲取上下文的class去創建對象)
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); // 獲取spring工廠的示例(通過獲取工廠集合中的class去一一創 建對象返回相應的對象集合)
prepareContext(context, environment, listeners, applicationArguments, printedBanner); // 準備上下文 { 設置上下文的環境;上下文對象配置;初始化上下文;監聽者上下文準備(遍歷監聽者集合為每個監聽者調用各自的contextPrepared()方法); 啟動日志信息; } refreshContext(context); // 刷新上下文對象
afterRefresh(context, applicationArguments); // 之后刷新
stopWatch.stop(); // 計時結束
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); // 開啟一個日志對象
}
listeners.started(context); // 監聽者開啟上下文(遍歷監聽者集合為每個監聽者調用各自的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;
}
暫未整理完;
SpringBoot 的 run方法
?著作權歸作者所有,轉載或內容合作請聯系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
推薦閱讀更多精彩內容
- SpringApplication.run()的實現原理。 return new SpringApplicatio...