Spring Boot的配置(組態檔,加載順序,配置原理)之組態檔
組態檔
Spring Boot使用一個全域組態檔,組態檔名是固定的
- application.properties
- application.yml
組態檔的作用:修改Spring Boot自動配置的默認值,即修改Spring Boot在底層都給我們配置好的值
YAML(YAML Ain't Markup Language)
標記語言:
? 以前的組態檔;大多都使用的是xxx.xml檔案;
? YAML:以資料為中心,比json、xml更適合做組態檔
? YAML配置例子(冒號后面一定要加空格)
server:
port: 8081
? XML:配置例子
<server>
<port>8081</port>
</server>
1.YAML的語法
1.基本語法
屬性與值的關系: k:(空格)v : 表示一對鍵值對(空格不能省略);
以空格的縮進來控制層級關系:只要是左對齊的資料都是都同一個層級的
server:
port: 8081
path: /hello
屬性和值也是大小寫敏感的;
2.值的寫法
字面量:普通的值(數字,字串,布爾)
k: v 字面直接來寫;
? 一般字串不用加上單引號或者雙引號;
? “雙引號”:會轉義字串里面的特殊字符,特殊字符會作為本身想標識的意思
name: "zhangsan /n list"
輸出:zhangsan 換行 list
? ‘單引號’:不會轉義特殊字符,特殊字符最終只是一個普通的字串資料
name: 'zhangsan /n list'
輸出:zhangsan /n list
物件、Map(屬性和值)(鍵值對):
? k: v 在下一行來寫物件的屬性和值的關系;注意使用空格縮進
friends:
lastName: zhangsan
age: 20
? 行內寫法:
friends: {lastName: zhangsan,age: 18}
陣列(List、Set):
用 -(空格)值 表示陣列中的一個元素
pets:
- cat
- dog
- pig
行內寫法
pets: {cat,dog,pig}
3.組態檔值注入
1.@ConfigurationProperties(默認從全域組態檔中取值)
組態檔
server:
port: 8081
person:
lastName: zhangsan
age: 19
boss: false
birth: 2020/5/20
maps: {k1: v1,k2: 12}
lists:
- lisi
- zhaoliu
dog:
name: 旺財
age: 2
javaBean
/**
* 將組態檔中配置的每一個屬性的值,映射到這個組件中
* @ConfigurationProperties:告訴springBoot將本類中的所有屬性和組態檔相關的配置進行系結;
* prefix = "person":組態檔中哪個下面的所有屬性進行一一映射
* 只有這個組件是容器中的組件,才能使用容器提供的功能;
**/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
匯入組態檔處理器,組態檔進行系結就會有提示(配置中的提示滿足駝峰命名法的切換lastName=last-name)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2.@Value
/**
* Spring底層的Value,之前是在Spring組態檔中
* <bean>
* <property name="lastName" value="https://www.cnblogs.com/JIATCODE/p/字面量 / ${key}-從環境變數、組態檔中獲取值 / #{spel}"><property/>
* </bean>
* 以前支持的,在@Value中都支持
**/
@Value("${person.last-name}")
private String lastName;
@Value("#{11*2}")
private Integer age;
@Value("true")
private Boolean boss;
3.@Value獲取值和@ConfigurationProperties獲取值比較
| @ConfigutationProperties | @Value | |
|---|---|---|
| 批量注入 | 支持 | 不支持 |
| 松散系結(松散語法:lastName=last_name=last-name) | 支持 | 不支持 |
| SpEL | 不支持 | 支持 |
| JSR303資料校驗 | 支持 | 不支持 |
| 復雜型別封裝(物件、map) | 支持 | 不支持 |
組態檔無論是ymal還是properties他們都能獲取到值
總結:
如果說只是在某個業務邏輯(Controller、Service)中獲取一下組態檔中的某項值,使用@Value
如果說我們專門撰寫了一個javabean來和組態檔進行映射,則使用@ConfigurationProperties
4.組態檔注入值資料校驗
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
@Email
private String lastName;
5.@PropertySource&ImportResource
- @PropertySource:加載指定的組態檔(如果全域組態檔中有符合的前綴屬性會先加載全域檔案,就不會再去尋找PropertySource標注的檔案)
@PropertySource("classpath:person.properties")
@Component
@ConfigurationProperties(prefix = "person")
- @ImportResource:匯入Spring的組態檔,讓組態檔里面的內容生效;
Spring Boot里面沒有Spring的組態檔,我們自己撰寫的組態檔,也不能自動識別;
想讓Spring的組態檔生效,加載進來;@ImportResource標注在一個配置類上
@ImportResource(locations = {"classpath:beans.xml"})
匯入Spring的組態檔讓其生效
-
Spring Boot中推薦的添加組件的方式
1.不撰寫xml檔案,使用全注解的方式(配置類==》組態檔)
2.使用@Bean注解來給容器中添加組件
/** * 指明當前類為配置類 * 在組態檔中用bean來添加組件 * 配置類中用@Bean **/ @Configuration public class MyAppConfig { //將方法的回傳值添加到容器中:容器中這個組件默認的id就是方法名 @Bean public HelloService helloService(){ System.out.println("@Bean給容器添加組建了"); return new HelloService(); } }
4.組態檔占位符
1.亂數
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
2.占位符獲取之前配置的值,如果沒有可以使用: 指定默認值
可以在組態檔中參考前面配置過的屬性(優先級前面配置過的這里都能用),找不到屬性時使用${key: value}來指定默認值
person.last-name=張三${random.uuid}
person.age=${random.int}
person.birth=2017/12/15
person.boss=false
person.dog.name=${person.hello: hello}_dog
person.dog.age=2
person.maps.k1=v1
person.maps.k2=15
person.lists=1,2,b
5.Profile
1.多Profile檔案
我們在主組態檔撰寫的時候,檔案名可以是application-{profile}.properties/yml
默認使用application.properties;
2.yml支持多檔案塊方式
三個 - 可以分出分檔塊
profiles: 指定屬于哪個環境(自定義),active:激活哪個環境
server:
port: 8081
spring:
profiles:
active: prod
---
server:
port: 8082
spring:
profiles: prod
---
server:
port: 8083
spring:
profiles: dev
---
3.激活指定profile
1.在默認全域組態檔中 spring.profiles.active=dev 激活
2.命令列的方式激活:
-
cmd在jar包目錄下 : java -jar xxx.jar --spring.profies.active=dev
-
IDEA運行配置的program arguments(程式引數輸入): --spring.profies.active=dev
-
IDEA運行配置的VM options(虛擬機選項):-Dspring.profiles.active=dev
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/177258.html
標籤:Java
下一篇:JVM系列.JDK演進歷史
