Spring Boot 配置檔案詳解
- 一、Spring Boot 配置檔案
-
- 1.1 配置檔案類型
- 1.2 自定義屬性
- 1.3 使用随機數
- 1.4 多環境配置
- 1.5 自定義配置檔案
一、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
[email protected]
顯示結果
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
[email protected]
# 随機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
,可以看到顯示如下内容: