源碼基於SpringBoot 2.4.4
1、認識配置文件
1.1 配置文件的加載
創建SpringBoot項目的時候,會自動創建一個application.properties文件,該文件是SpringBoot默認的配置文件。
SpringBoot在啟動的時候會默認去尋找並加載application.properties文件和application.yaml文件,在以下路徑中尋找:
(1)classpath目錄下
(2)classpath目錄下的config文件夾
(3)項目根目錄
(4)項目根目錄下config文件夾的子文件夾
(5)項目根目錄下config文件夾

優先級從高到低,並且后加載的會覆蓋前面加載的。
1.2 配置文件類型
(1)properties后綴:application.properties
(2)yaml后綴:application.yaml
1.3 兩種配置文件比較
- 可以使用 @PropertySource 注解加載自定義的 Properties 配置文件,但無法加載自定義的 YAML 文件。
- 配置文件加載順序:yaml、yml、xml、properties(后加載的會覆蓋前面加載的)
2、yaml配置文件
2.1 介紹
YAML 是 "YAML Ain't Markup Language"(YAML 不是一種標記語言)的遞歸縮寫。在開發的這種語言時,YAML 的意思其實是:"Yet Another Markup Language"(仍是一種標記語言)。
非常適合用來做以數據為中心的配置文件
2.2 基本語法
(1)key: value。冒號與value之間必須有一個空格。
(2)大小寫敏感。
(3)使用縮進表示層級關系。
(4)縮進不允許使用tab,只能使用空格。
(5)縮進的空格數不重要,只要相同層級的元素左對齊即可。
(6)# 后面跟注釋內容
(7)字符串無須加引號。""會進行轉義。比如'\n'就輸出\n,但"\n"則會輸出換行。
2.3 數據類型
(1)字面量:單個的、不可再分的值。date、boolean、string、number、null
k: v
(2)對象:鍵值對的集合。map、hash、set、object
# 行內寫法
k: {k1:v1,k2:v2,k3:v3}
# 或
k:
k1: v1
k2: v2
k3: v3
(3)數組:一組按次序排列的值。array、list、queue
# 行內寫法
k: [v1,v2,v3]
# 或
k:
- v1
- v2
- v3
2.4 實例
定義兩個實體類Person和Pet,並且Person類和配置文件的屬性綁定
@Data
@ConfigurationProperties(prefix = "my-person")
@Component
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private Pet pet;
private String[] interests;
private List<String> animal;
private Map<String, Object> score;
private Set<Double> salarys;
private Map<String, List<Pet>> allPets;
}
@Data
public class Pet {
private String name;
private Double weight;
}
在yaml文件中配置Person的屬性
my-person:
user-name: CodeTiger
boss: true
birth: 1996/11/29
# 可以使用random來生成各種不同類型的隨機值
age: ${random.int}
pet:
name: tomcat
weight: 100
interests: [basketball, football]
animal:
- jerry
- tom
score:
english:
first: 30
second: 40
third: 50
math: [131,140,148]
chinese: {first: 128, second: 136}
salarys: [3999,4999.98,5999.99]
allPets:
sick:
- {name: tom}
- {name: jerry,weight: 47}
health: [{name: mario,weight: 47}]
啟動SpringBoot,可以打印出Person實體類的屬性值。如果在application.properties中配置Person的一些字段值,那么會如何加載呢?
my-person.interests=basketball, football, pingpang
my-person.user-name=lxp
my-person.score.chinese=100
my-person.score.english=99
啟動SpringBoot,打印Person
Person(userName=lxp, boss=true, birth=Fri Nov 29 00:00:00 CST 1996, age=16165848, pet=Pet(name=tomcat, weight=100.0), interests=[basketball, football, pingpang], animal=[jerry, tom], score={english=99, chinese=100, math={0=131, 1=140, 2=148}}, salarys=[3999.0, 4999.98, 5999.99], allPets={sick=[Pet(name=tom, weight=null), Pet(name=jerry, weight=47.0)], health=[Pet(name=mario, weight=47.0)]})
會發現application.properties中配置的屬性會覆蓋掉application.yaml中配置的屬性,並且如果該屬性有多個字段時,會進行合並,比如score={english=99, chinese=100, math={0=131, 1=140, 2=148}}就是兩個配置文件合並后的結果。
2.5 開啟自動提示
我們的類和配置文件屬性綁定時,在配置文件中設置屬性發現並沒有自動提示。在我們為類加上@ConfigurationProperties注解的時候,idea就會提示我們配置Annotation Processor

根據文檔,我們只需要在pom.xml中增加相關的jar包即可。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
在打包的時候,我們不需要把它打入jar包,所以去除
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
3、使用yaml實現多環境配置
我們可以把多個環境的配置寫在一個文件里,可以使用符號---進行分割,如下application.yaml文件
spring:
profiles:
# 使用開發環境的配置
active: dev
---
# 開發環境配置
server:
port: 8888
spring:
profiles: dev
---
# 生產環境配置
server:
port: 8888
spring:
profiles: prod
也可以不同的開發環境寫在不同的配置文件,比如在resources目錄下創建兩個配置文件:application-dev.yml 和 application-prod.yml 此時,分別表示開發環境中的配置和生產環境中的配置。

然后在application.yaml文件中指定使用哪個配置文件即可。
我們還可以通過在代碼中指定使用哪種環境的配置
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(TestApplication.class);
builder.application().setAdditionalProfiles("dev");
builder.run(args);
}
}
當然也可以在使用命令啟動jar包的時候指定
java -jar xxx.jar --spring.profiles.active=dev
