Springboot詳解底層啟動(dòng)過程_第1頁
Springboot詳解底層啟動(dòng)過程_第2頁
Springboot詳解底層啟動(dòng)過程_第3頁
Springboot詳解底層啟動(dòng)過程_第4頁
Springboot詳解底層啟動(dòng)過程_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第Springboot詳解底層啟動(dòng)過程目錄SpringApplication構(gòu)造分析SpringApplicationrun分析

SpringApplication構(gòu)造分析

1、記錄BeanDefinition源

spring容器剛開始是空的,要去各個(gè)源找到beanDefinition,這些源可能是配置類,可能是xml文件。在構(gòu)造方法里會(huì)獲取一個(gè)主源,也就是引導(dǎo)類,根據(jù)引導(dǎo)類去獲取beanDefinition。

2、推斷應(yīng)用類型

根據(jù)jar包去判斷是什么引用類型

3、記錄ApplicationContext初始化器

對(duì)ApplicationContext做擴(kuò)展

4、記錄監(jiān)聽器

監(jiān)聽重要事件

5、推斷主啟動(dòng)類

記錄運(yùn)行的主類。

SpringApplicationrun分析

1、得到SpringApplicationRunListeners,名字取得不好,實(shí)際是事件發(fā)布器

發(fā)布applicationstarting事件,在程序啟動(dòng)的重要節(jié)點(diǎn)發(fā)布事件

publicstaticvoidmain(String[]args)throwsException{

//添加app監(jiān)聽器

SpringApplicationapp=newSpringApplication();

app.addListeners(e-System.out.println(e.getClass()));

//獲取事件發(fā)送器實(shí)現(xiàn)類名

ListStringnames=SpringFactoriesLoader.loadFactoryNames(SpringApplicationRunListener.class,A39_2.class.getClassLoader());

for(Stringname:names){

System.out.println(name);

Classclazz=Class.forName(name);

Constructorconstructor=clazz.getConstructor(SpringApplication.class,String[].class);

SpringApplicationRunListenerpublisher=(SpringApplicationRunListener)constructor.newInstance(app,args);

//發(fā)布事件

DefaultBootstrapContextbootstrapContext=newDefaultBootstrapContext();

publisher.starting(bootstrapContext);//springboot開始啟動(dòng)

publisher.environmentPrepared(bootstrapContext,newStandardEnvironment());//環(huán)境信息準(zhǔn)備完畢

GenericApplicationContextcontext=newGenericApplicationContext();

publisher.contextPrepared(context);//在spring容器創(chuàng)建,并調(diào)用初始化器之后,發(fā)送此事件

publisher.contextLoaded(context);//所有beandefinition加載完畢

context.refresh();

publisher.started(context);//spring容器初始化完成(refresh方法調(diào)用完畢)

publisher.running(context);//springboot啟動(dòng)完畢

publisher.failed(context,newException("出錯(cuò)了"));//springboot啟動(dòng)出錯(cuò)

}

2、封裝啟動(dòng)args

3、準(zhǔn)備Environment添加命令行參數(shù)(*)

publicstaticvoidmain(String[]args)throwsIOException{

ApplicationEnvironmentenv=newApplicationEnvironment();//系統(tǒng)環(huán)境變量,properties,yaml

env.getPropertySources().addLast(newResourcePropertySource(newClassPathResource("perties")));

env.getPropertySources().addFirst(newSimpleCommandLinePropertySource(args));

for(PropertySourceps:env.getPropertySources()){

System.out.println(ps);

//System.out.println(env.getProperty("JAVA_HOME"));

System.out.println(env.getProperty("server.port"));

}

4、ConfigurationPropertySources處理(*)

發(fā)布applicationenvironment已準(zhǔn)備事件

publicstaticvoidmain(String[]args)throwsIOException,NoSuchFieldException{

ApplicationEnvironmentenv=newApplicationEnvironment();

env.getPropertySources().addLast(

newResourcePropertySource("step4",newClassPathResource("perties"))

ConfigurationPropertySources.attach(env);

for(PropertySourceps:env.getPropertySources()){

System.out.println(ps);

System.out.println(env.getProperty("user.first-name"));

System.out.println(env.getProperty("user.middle-name"));

System.out.println(env.getProperty("user.last-name"));

}

5、通過EnvironmentPostProcessorApplicationListener進(jìn)行env后處理(*)

perties,由StandardConfigDataLocationResolver解析

spring.application.json

publicclassStep5{

publicstaticvoidmain(String[]args){

SpringApplicationapp=newSpringApplication();

app.addListeners(newEnvironmentPostProcessorApplicationListener());

/*ListStringnames=SpringFactoriesLoader.loadFactoryNames(EnvironmentPostProcessor.class,Step5.class.getClassLoader());

for(Stringname:names){

System.out.println(name);

EventPublishingRunListenerpublisher=newEventPublishingRunListener(app,args);

ApplicationEnvironmentenv=newApplicationEnvironment();

System.out.println("增強(qiáng)前");

for(PropertySourceps:env.getPropertySources()){

System.out.println(ps);

publisher.environmentPrepared(newDefaultBootstrapContext(),env);

System.out.println("增強(qiáng)后");

for(PropertySourceps:env.getPropertySources()){

System.out.println(ps);

privatestaticvoidtest1(){

SpringApplicationapp=newSpringApplication();

ApplicationEnvironmentenv=newApplicationEnvironment();

System.out.println("增強(qiáng)前");

for(PropertySourceps:env.getPropertySources()){

System.out.println(ps);

ConfigDataEnvironmentPostProcessorpostProcessor1=newConfigDataEnvironmentPostProcessor(newDeferredLogs(),newDefaultBootstrapContext());

postProcessor1.postProcessEnvironment(env,app);

System.out.println("增強(qiáng)后");

for(PropertySourceps:env.getPropertySources()){

System.out.println(ps);

RandomValuePropertySourceEnvironmentPostProcessorpostProcessor2=newRandomValuePropertySourceEnvironmentPostProcessor(newDeferredLog());

postProcessor2.postProcessEnvironment(env,app);

System.out.println("增強(qiáng)后");

for(PropertySourceps:env.getPropertySources()){

System.out.println(ps);

System.out.println(env.getProperty("server.port"));

System.out.println(env.getProperty(""));

System.out.println(env.getProperty(""));

System.out.println(env.getProperty(""));

System.out.println(env.getProperty("random.uuid"));

System.out.println(env.getProperty("random.uuid"));

System.out.println(env.getProperty("random.uuid"));

}

6、綁定spring.main到SpringApplication對(duì)象(*)

把配置文件中的值賦給SpringApplication的默認(rèn)屬性值

publicclassStep6{

//綁定spring.main前綴的keyvalue至SpringApplication,請(qǐng)通過debug查看

publicstaticvoidmain(String[]args)throwsIOException{

SpringApplicationapplication=newSpringApplication();

ApplicationEnvironmentenv=newApplicationEnvironment();

env.getPropertySources().addLast(newResourcePropertySource("step6",newClassPathResource("perties")));

System.out.println(application);

Binder.get(env).bind("spring.main",Bindable.ofInstance(application));

System.out.println(application);

}

7、打印banner(*)

publicclassStep7{

publicstaticvoidmain(String[]args){

ApplicationEnvironmentenv=newApplicationEnvironment();

SpringApplicationBannerPrinterprinter=newSpringApplicationBannerPrinter(

newDefaultResourceLoader(),

newSpringBootBanner()

//測(cè)試文字banner

//env.getPropertySources().addLast(newMapPropertySource("custom",Map.of("spring.banner.location","banner1.txt")));

//測(cè)試圖片banner

//env.getPropertySources().addLast(newMapPropertySource("custom",Map.of("spring.banner.image.location","banner2.png")));

//版本號(hào)的獲取

System.out.println(SpringBootVersion.getVersion());

printer.print(env,Step7.class,System.out);

}

8、創(chuàng)建容器

privatestaticGenericApplicationContextcreateApplicationContext(WebApplicationTypetype){

GenericApplicationContextcontext=null;

switch(type){

caseSERVLET-context=newAnnotationConfigServletWebServerApplicationContext();

caseREACTIVE-context=newAnnotationConfigReactiveWebServerApplicationContext();

caseNONE-context=new

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論