spring boot環境初始化過程

2021-09-10 16:36:30 字數 3110 閱讀 9198

先看一下最簡單的spring boot中環境中有些什麼東西,首先spring boot的版本是1.5.9

@restcontroller

public class helloworld

system.out.println("qwe");

for (string s : environment.getdefaultprofiles())

return "qwe";}}

測試**

這是環境裡面所有的變數,接下來就看看這些環境變數是從**生成的。首先可以從run的方法中進入,可以看到prepareenvironment方法用來生成環境。

configurableenvironment environment = this.getorcreateenvironment();

listeners.environmentprepared((configurableenvironment)environment);

if (!this.webenvironment)

return (configurableenvironment)environment;

}只需要看3個方法就可以getorcreateenvironment,configureenvironment,environmentprepared,先來看看getorcreateenvironment

private configurableenvironment getorcreateenvironment()  else 

}

這裡只做了一步,new standardservletenvironment()。接下來看看會初始化一些什麼東西

standardservletenvironment->standardenvironment->abstractenvironment。這是乙個繼承關係,先去初始化abstractenvironment中的建構函式

public abstractenvironment() 

}

customizepropertysources方法進行初始化,接下來在standardservletenvironment找到了方法

protected void customizepropertysources(mutablepropertysources propertysources) 

super.customizepropertysources(propertysources);

}

這裡初始化了servlet的一些引數。這裡還呼叫了父類的方法,接下來看看父類中的方法

protected void customizepropertysources(mutablepropertysources propertysources)
發現這裡初始化了系統環境變數。

至此大部分的初始化已經完成。

接下來看看configureenvironment

protected void configureenvironment(configurableenvironment environment, string args)
進入第乙個方法

protected void configurepropertysources(configurableenvironment environment, string args) 

if (this.addcommandlineproperties && args.length > 0) else

}}

這裡是進行命令列引數的初始化

listpostprocessors = this.loadpostprocessors();

postprocessors.add(this);

annotationawareordercomparator.sort(postprocessors);

iterator var3 = postprocessors.iterator();

while(var3.hasnext())

}進入postprocessenvironment方法

this.configureignorebeaninfo(environment);

}進入addpropertysources方法

protected void addpropertysources(configurableenvironment environment, resourceloader resourceloader)
在這裡randomvaluepropertysource新增了環境變數

接下來就差最後乙個.yml和.properties檔案的載入了。

進入load()方法

public void load() }}

this.profiles.add((object)null);

label41:

for(; !this.profiles.isempty(); this.processedprofiles.add(profile))

string location = (string)var8.next();

if (!location.endswith("/")) else }}

}}

this.addconfigurationproperties(this.propertiesloader.getpropertysources());

}

再看看內部的load方法

string group = "profile=" + (profile == null ? "" : profile);

string ext;

if (!stringutils.hastext(name)) else

}this.loadintogroup(group, location + name + "-" + profile + "." + ext, profile);}}

}}

可以看到在for迴圈的內部有乙個this.loadintogroup(group, location + name + "." + ext, profile)

spring boot初始化報錯

解決方法 將pom檔案中新增如下依賴 org.springframework.boot spring boot starter tomcat 1 234另,如果不行,嘗試再將下面依賴 org.springframework.boot spring boot starter 1 234改為 org.s...

springboot資料初始化

我們常利用springboot資料初始化進行測試資料準備,或者專案啟動的資料準備等,下面來具體了解下配置的含義。springboot2資料初始化配置如下 spring.datasource.initialization mode always spring.datasource.schema cla...

Spring Boot的初始化過程

public return public 一系列的初始化 initialize sources private void initialize object sources this.webenvironment deducewebenvironment 設定初始化物件,後期在preparecont...