Spring Boot 配置文件詳解
一、Spring Boot 配置文件
1.1 配置文件類型
1.1.1 配置文件介紹
在使用IntelliJ IDEA創建Spring Boot項目時,IDE會在src/main/java/resources
目錄下創建一個application.properties
文件。
該文件是 SpringBoot 的配置文件,所有的配置信息都在該文件中配置,包括對服務器的配置、spring 的配置、springmvc 的配置、mybatis 的配置等等。
這個配置文件也可以換成爲applicaiton.yml
文件,application.yml
配置文件擁有更爲簡潔的配置語法,但是對書寫的格式要求更加嚴格,不過無論是用那種配置文件最終效果都是一致的。
1.1.2 配置文件語法
(1)application.properties
文件語法
示例
server.port=8080
server.error.include-stacktrace=always
server.error.include-exception=true
server.compression.enabled=true
spring.resources.static-locations=classpath:/static/, classpath:/templates/
spring.resources.chain.cache=true
spring.resources.chain.compressed=true
spring.aop.auto=true
顯示效果
(2)application.yml
文件語法
示例
server:
port: 8080
error:
include-stacktrace: always
include-exception: true
compression:
enabled: true
spring:
resources:
static-locations: classpath:/static/, classpath:/templates/
chain:
cache: true
compressed: true
aop:
auto: true
顯示效果
2.3.4 YAML
YAML(YAML Ain’t Markup Language),以數據爲中心,比json, xm|等更適合做配置文件。
示例
server:
port: 8080
spring:
resources:
static-locations: classpath:/static/
說明
(1)k(空格)v
:表示一堆鍵值對,中間以空格隔開,以空格的縮進來控制層級關係,只要左對齊的一列數據都是屬於同一個層級的。
(2)數字、字符串、布爾值的寫法:k(空格)v
,字符串默認不用加上單引號或者雙引號
- 雙引號不會轉義字符串裏面的符號,而是會以其本身的意思來表達
- 單引號會將引號內的符號進行轉譯。
(3)對象、Map(屬性和值)的寫法:k(空格)v
,對象仍舊是以鍵值對的方式屬性
student:
name: zhangsan
age: 18
或者
student: {name: zhangsan,age: 18}
(3)數組(List、Set)
用-
來表示數組中的元素
person:
- girl
- boy
- others
或者
person: [girl,boy,others]
注意:yml 文件書寫格式要求非常嚴格,具體要求如下:
- 絕對不能使用tab鍵來創建空格,如果下一行是一個子級別的屬性名按回車後輸入一個空格然後繼續填寫屬性值。
- 文件的最後不能有空行(無內容的行),有的話通過delete鍵刪除
- 每個
:
後邊都要空一格(注意一定要用空格鍵來操作,不能用Tab鍵來空格) - 每個級別的屬性名要嚴格對齊
- 配置文件中間也不能有空行(無內容的行)
1.2 自定義屬性
1.2.1 自定義屬性示例
配置文件除了有系統的屬性外,還可以自定義屬性,以application.properties
配置文件爲例進行示例。
自定義示例
student.name=張三
student.age=18
student.sex=Man
student.email=11234567@163.com
顯示結果
1.2.2 讀取自定義屬性
在類中,如果需要讀取配置文件的內容,那麼只需要在屬性上使用@Value("${屬性名}")
,新建一個TestController,在其中創建一個test方法進行測試。
TestController的內容如下。
@RestController
public class TestController {
@Value("${student.name}")
private String name;
@Value("${student.age}")
private String age;
@Value("${student.name}")
private String sex;
@Value("${student.name}")
private String email;
@GetMapping("test")
public String test(){
return "student.name = " + name +
" student.age = " + age +
" student.sex = " + sex +
" student.email = " + email;
}
}
網頁訪問http://localhost:8080/test
,得到如下結果
在application.properties
中配置中文值,讀取時會出現中文亂碼問題。因爲Java默認會使用ISO-8859-1的編碼方式來讀取*.properties
配置文件,而SpringBoot應用則以UTF-8的編碼方式來讀取,就導致產生了亂碼問題。
對於這個問題,官方推薦的做法是:“Characters that cannot be directly represented in this encoding can be written using Unicode escapes”,大致意思就是使用Unicode的方式來展示字符。例如上述代碼中的student.name=張三
應該配置成student.name=\u5f20\u4e09
。
再次訪問得到如下結果
1.3 使用隨機數
1.3.1 使用隨機數示例
在配置文件中,還提供了隨機數供我們使用,即在配置文件中使用${random}
來生成不同類型的隨機數,大致分爲隨機數、隨機uuid、隨機字符串等。
示例
Student.name=\u5f20\u4e09
student.age=18
student.sex=Man
student.email=11234567@163.com
# 隨機uuid
test.uuid=${random.uuid}
# 隨機int值
test.intNum=${random.int}
# 100以內的隨機int值
test.randomNum=${random.int(100)}
# 隨機long值
test.longNum=${random.long}
# 隨機字符串
test.value=${random.value}
# 自定義屬性間引用
test.name=${student.name}
test.age=${student.age}
test.sex=${student.sex}
test.email=${student.email}
顯示結果
1.3.2 獲取隨機屬性
在配置屬性後,可以使用JavaBean模式來給屬性賦值,創建一個ConfigBean實體類。由於自定義屬性的前綴都是由test開頭的,因此我們可以在實體類上加入註解@ConfigurationProperties(prefix = "test")
,同時需要在啓動類上加入註解@EnableConfigurationProperties(ConfigBean.class)
,表明啓動這個配置類。
ConfigBean實體類
@ConfigurationProperties(prefix = "test")
public class ConfigBean {
private String uuid;
private int intNum;
private int randomNum;
private long longNum;
private String value;
private String name;
private int age;
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getIntNum() {
return intNum;
}
public void setIntNum(int intNum) {
this.intNum = intNum;
}
public int getRandomNum() {
return randomNum;
}
public void setRandomNum(int randomNum) {
this.randomNum = randomNum;
}
public long getLongNum() {
return longNum;
}
public void setLongNum(long longNum) {
this.longNum = longNum;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
@ConfigurationProperties
註解對應的依賴如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
在TestController中利用@Autowired
註解注入ConfigBean類,並且創建一個test方法進行測試。
@RestController
public class TestController {
@Autowired
private ConfigBean configBean;
@GetMapping("test")
public ConfigBean test(){
return configBean;
}
}
在瀏覽器上訪問http://localhost:8080/test進行測試,顯示結果如下:
1.4 多環境配置
1.4.1 多環境配置介紹
在開發Spring Boot項目的時候,可能有這樣的情況,一套程序需要在不同的環境中發佈,數據庫配置、端口配置或者其他配置各不相同,如果每次都需要修改爲對應環境配置,不僅耗費人力,而且特別容易出現錯誤,造成不必要的麻煩。
通常情況下,我們可以配置多個配置文件,在不同的情況下進行替換。而在Spring Boot項目中,我們新建幾個配置文件,文件名以application-{name}.properties
的格式,其中的{name}
對應環境標識,比如:
- application-dev.properties:開發環境。
- application-test.properties:測試環境。
- application-prod.properties:生產環境。
-
- application-local.properties:本地環境。
然後,可以在主配置文件(application.properties
)中配置spring.profiles.active
來設置當前要使用的配置文件,比如:
spring.profiles.active=dev
1.4.2 多環境配置示例
創建application-dev.properties
配置文件,在文件中配置端口號爲8081。
創建application-test.properties
配置文件,在文件中配置端口號爲8082。
創建application-prod.properties
配置文件,在文件中配置端口號爲8083。
創建application-local.properties
配置文件,在文件中配置端口號爲8084。
在application.properties
配置文件中設置當前要使用的配置文件
啓動項目,可以在控制檯看到當前啓動的端口
1.5 自定義配置文件
1.5 自定義配置文件示例
前面介紹了多環境配置文件,我們也可以使用自定義配置文件,比如新建一個test.properties
,配置文件內容下。
1.5.2 獲取自定義配置文件的內容
新建一個javabean來讀取配置文件。新建一個ConfigBean,在類上加上註解@PropertySource(value = "classpath:test.properties")
,並且和之前一樣需要加入@ConfigurationProperties(prefix = "test")
@RestController
public class TestController {
@Autowired
private ConfigBean configBean;
@GetMapping("test")
public ConfigBean test(){
return configBean;
}
}
在TestController中注入bean並且創建測試方法test
@Component
@ConfigurationProperties(prefix = "test")
@PropertySource(value = "classpath:test.properties")
public class ConfigBean {
private String uuid;
private int intNum;
private int randomNum;
private long longNum;
private String value;
private String name;
private int age;
private String sex;
private String email;
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getIntNum() {
return intNum;
}
public void setIntNum(int intNum) {
this.intNum = intNum;
}
public int getRandomNum() {
return randomNum;
}
public void setRandomNum(int randomNum) {
this.randomNum = randomNum;
}
public long getLongNum() {
return longNum;
}
public void setLongNum(long longNum) {
this.longNum = longNum;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
使用瀏覽器訪問http://localhost:8080/test
,可以看到顯示如下內容: