SpringBoot配置文件詳解

只是轉(zhuǎn)載,怕找不到 轉(zhuǎn)載自https://www.cnblogs.com/charleswone/p/11437661.html


1、配置文件

SpringBoot使用一個(gè)全局的配置文件,配置文件名是固定的;

?application.properties

?application.yml

配置文件的作用:修改SpringBoot自動(dòng)配置的默認(rèn)值;SpringBoot在底層都給我們自動(dòng)配置好;

YAML(YAML Ain't Markup Language)

? YAML A Markup Language:是一個(gè)標(biāo)記語言

? YAML isn't Markup Language:不是一個(gè)標(biāo)記語言;

標(biāo)記語言:

? 以前的配置文件;大多都使用的是?xxxx.xml文件;

? YAML:以數(shù)據(jù)為中心,比json、xml等更適合做配置文件;

? YAML:配置例子

server:? port:8081

? XML:

8081

2、YAML語法:

1、基本語法

k:(空格)v:表示一對(duì)鍵值對(duì)(空格必須有);

空格的縮進(jìn)來控制層級(jí)關(guān)系;只要是左對(duì)齊的一列數(shù)據(jù),都是同一個(gè)層級(jí)的

server:? ? port:8081? ? path:/hello

屬性和值也是大小寫敏感;

2、值的寫法

字面量:普通的值(數(shù)字,字符串,布爾)

? k: v:字面直接來寫;

? 字符串默認(rèn)不用加上單引號(hào)或者雙引號(hào);

? "":雙引號(hào);不會(huì)轉(zhuǎn)義字符串里面的特殊字符;特殊字符會(huì)作為本身想表示的意思

? name: "zhangsan \n lisi":輸出;zhangsan 換行 lisi

? '':?jiǎn)我?hào);會(huì)轉(zhuǎn)義特殊字符,特殊字符最終只是一個(gè)普通的字符串?dāng)?shù)據(jù)

? name: ‘zhangsan \n lisi’:輸出;zhangsan \n lisi

對(duì)象、Map(屬性和值)(鍵值對(duì)):

? k: v:在下一行來寫對(duì)象的屬性和值的關(guān)系;注意縮進(jìn)

? 對(duì)象還是k: v的方式

friends:lastName:zhangsanage:20

行內(nèi)寫法:

friends:{lastName:zhangsan,age:18}

數(shù)組(List、Set):

用- 值表示數(shù)組中的一個(gè)元素

pets: -cat -dog -pig

行內(nèi)寫法

pets:[cat,dog,pig]

3、配置文件值注入

配置文件

person:? ? lastName:hello? ? age:18? ? boss:false? ? birth:2017/12/12? ? maps:{k1:v1,k2:12}? ? lists:? ? ? -lisi? ? ? -zhaoliu? ? dog:? ? ? name:小狗? ? ? age:12

javaBean:

/** * 將配置文件中配置的每一個(gè)屬性的值,映射到這個(gè)組件中 *@ConfigurationProperties:告訴SpringBoot將本類中的所有屬性和配置文件中相關(guān)的配置進(jìn)行綁定; *? ? ? prefix = "person":配置文件中哪個(gè)下面的所有屬性進(jìn)行一一映射 * * 只有這個(gè)組件是容器中的組件,才能容器提供的@ConfigurationProperties功能; * */@Component@ConfigurationProperties(prefix ="person")publicclassPerson{privateString lastName;privateInteger age;privateBoolean boss;privateDate birth;privateMap maps;privateList lists;privateDog dog;

我們可以導(dǎo)入配置文件處理器,以后編寫配置就有提示了

<!--導(dǎo)入配置文件處理器,配置文件進(jìn)行綁定就會(huì)有提示-->org.springframework.bootspring-boot-configuration-processortrue

1、properties配置文件在idea中默認(rèn)utf-8可能會(huì)亂碼

調(diào)整

2、@Value獲取值和@ConfigurationProperties獲取值比較

@ConfigurationProperties@Value

功能批量注入配置文件中的屬性一個(gè)個(gè)指定

松散綁定(松散語法)支持不支持

SpEL不支持支持

JSR303數(shù)據(jù)校驗(yàn)支持不支持

復(fù)雜類型封裝支持不支持

配置文件yml還是properties他們都能獲取到值;

如果說,我們只是在某個(gè)業(yè)務(wù)邏輯中需要獲取一下配置文件中的某項(xiàng)值,使用@Value;

如果說,我們專門編寫了一個(gè)javaBean來和配置文件進(jìn)行映射,我們就直接使用@ConfigurationProperties;

3、配置文件注入值數(shù)據(jù)校驗(yàn)

@Component@ConfigurationProperties(prefix ="person")@ValidatedpublicclassPerson{/**

? ? * <bean class="Person">

? ? *? ? ? <property name="lastName" value="字面量/${key}從環(huán)境變量、配置文件中獲取值/#{SpEL}"></property>

? ? * <bean/>

? ? *///lastName必須是郵箱格式@Email//@Value("${person.last-name}")privateString lastName;//@Value("#{11*2}")privateInteger age;//@Value("true")privateBoolean boss;privateDate birth;privateMap maps;privateList lists;privateDog dog;

4、@PropertySource&@ImportResource&@Bean

@PropertySource:加載指定的配置文件;

/** * 將配置文件中配置的每一個(gè)屬性的值,映射到這個(gè)組件中 *@ConfigurationProperties:告訴SpringBoot將本類中的所有屬性和配置文件中相關(guān)的配置進(jìn)行綁定; *? ? ? prefix = "person":配置文件中哪個(gè)下面的所有屬性進(jìn)行一一映射 * * 只有這個(gè)組件是容器中的組件,才能容器提供的@ConfigurationProperties功能; *@ConfigurationProperties(prefix = "person")默認(rèn)從全局配置文件中獲取值; * */@PropertySource(value = {"classpath:person.properties"})@Component@ConfigurationProperties(prefix ="person")//@ValidatedpublicclassPerson{/**

? ? * <bean class="Person">

? ? *? ? ? <property name="lastName" value="字面量/${key}從環(huán)境變量、配置文件中獲取值/#{SpEL}"></property>

? ? * <bean/>

? ? *///lastName必須是郵箱格式// @Email//@Value("${person.last-name}")privateString lastName;//@Value("#{11*2}")privateInteger age;//@Value("true")privateBoolean boss;

@ImportResource:導(dǎo)入Spring的配置文件,讓配置文件里面的內(nèi)容生效;

Spring Boot里面沒有Spring的配置文件,我們自己編寫的配置文件,也不能自動(dòng)識(shí)別;

想讓Spring的配置文件生效,加載進(jìn)來;@ImportResource標(biāo)注在一個(gè)配置類上

@ImportResource(locations = {"classpath:beans.xml"})導(dǎo)入Spring的配置文件讓其生效

不來編寫Spring的配置文件

<?xml version="1.0"encoding="UTF-8"?>

SpringBoot推薦給容器中添加組件的方式;推薦使用全注解的方式

1、配置類@Configuration------>Spring配置文件

2、使用@Bean給容器中添加組件

/** *@Configuration:指明當(dāng)前類是一個(gè)配置類;就是來替代之前的Spring配置文件 * * 在配置文件中用標(biāo)簽添加組件 * */@ConfigurationpublicclassMyAppConfig{//將方法的返回值添加到容器中;容器中這個(gè)組件默認(rèn)的id就是方法名@BeanpublicHelloServicehelloService02(){? ? ? ? System.out.println("配置類@Bean給容器中添加組件了...");returnnewHelloService();? ? }}

4、配置文件占位符

1、隨機(jī)數(shù)

${random.value}、${random.int}、${random.long}${random.int(10)}、${random.int[1024,65536]}

2、占位符獲取之前配置的值,如果沒有可以是用:指定默認(rèn)值

person.last-name=張三${random.uuid}

person.age=${random.int}

person.birth=2017/12/15

person.boss=false

person.maps.k1=v1

person.maps.k2=14

person.lists=a,b,c

person.dog.name=${person.hello:hello}_dog

person.dog.age=15

5、Profile

1、多Profile文件

我們?cè)谥髋渲梦募帉懙臅r(shí)候,文件名可以是 application-{profile}.properties/yml

默認(rèn)使用application.properties的配置;

2、yml支持多文檔塊方式

server:? port:8081spring:? profiles:? ? active:prod---server:? port:8083spring:? profiles:dev---server:? port:8084spring:? profiles:prod#指定屬于哪個(gè)環(huán)境

3、激活指定profile

? 1、在配置文件中指定 spring.profiles.active=dev

? 2、命令行:

? java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;

? 可以直接在測(cè)試的時(shí)候,配置傳入命令行參數(shù)

? 3、虛擬機(jī)參數(shù);

? -Dspring.profiles.active=dev

6、配置文件加載位置

springboot 啟動(dòng)會(huì)掃描以下位置的application.properties或者application.yml文件作為Spring boot的默認(rèn)配置文件

–file:./config/

–file:./

–classpath:/config/

–classpath:/

優(yōu)先級(jí)由高到底,高優(yōu)先級(jí)的配置會(huì)覆蓋低優(yōu)先級(jí)的配置;

SpringBoot會(huì)從這四個(gè)位置全部加載主配置文件;互補(bǔ)配置

我們還可以通過spring.config.location來改變默認(rèn)的配置文件位置

項(xiàng)目打包好以后,我們可以使用命令行參數(shù)的形式,啟動(dòng)項(xiàng)目的時(shí)候來指定配置文件的新位置;指定配置文件和默認(rèn)加載的這些配置文件共同起作用形成互補(bǔ)配置;

java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.location=G:/application.properties

7、外部配置加載順序

SpringBoot也可以從以下位置加載配置; 優(yōu)先級(jí)從高到低;高優(yōu)先級(jí)的配置覆蓋低優(yōu)先級(jí)的配置,所有的配置會(huì)形成互補(bǔ)配置

1.命令行參數(shù)

所有的配置都可以在命令行上進(jìn)行指定

java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --server.port=8087 --server.context-path=/abc

多個(gè)配置用空格分開; --配置項(xiàng)=值

2.來自java:comp/env的JNDI屬性

3.Java系統(tǒng)屬性(System.getProperties())

4.操作系統(tǒng)環(huán)境變量

5.RandomValuePropertySource配置的random.*屬性值

由jar包外向jar包內(nèi)進(jìn)行尋找;

優(yōu)先加載帶profile

6.jar包外部的application-{profile}.properties或application.yml(帶spring.profile)配置文件

7.jar包內(nèi)部的application-{profile}.properties或application.yml(帶spring.profile)配置文件

再來加載不帶profile

8.jar包外部的application.properties或application.yml(不帶spring.profile)配置文件

9.jar包內(nèi)部的application.properties或application.yml(不帶spring.profile)配置文件

10.@Configuration注解類上的@PropertySource

11.通過SpringApplication.setDefaultProperties指定的默認(rèn)屬性

所有支持的配置加載來源;

參考官方文檔

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

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