spring boot(3):配置檔案
一、springboot配置檔案類型
1.springboot配置檔案類型和作用
SpringBoot是基于約定的,是以很多配置都有預設值,但如果想使用自己的配置來替換預設配置,就可以使用application.properties或者application.yml(application.yaml)進行配置。
例如端口号,項目路徑,資料源等。
SpringBoot預設會從Resources目錄下加載application.properties或者application.yml(application.yaml)檔案。其中properties檔案是用鍵值對形式存儲,大家應該都會用,下面說一下yml檔案。
配置檔案的優先級
properties>yaml>yml(因為最後加載的是properties檔案,是以會覆寫掉之前的yml配置檔案)
2.application.yml配置檔案
springboot預設加載resources檔案夾下面的application開頭的yml或properties檔案。
-
yml檔案簡介
yml檔案是YAML(YAML Aint Markup Language)編寫的檔案格式,YAML是一種直覺的能夠被電腦識别的資料序列化格式,并且容易被人類閱讀,容易和腳本語言互動的,其中被支援YAML庫的不同的程式設計語言程式導入,比如C/C++,Ruby,Python.Perl,C#,PHP等。YML檔案是以資料為核心,比傳統的xml檔案更加簡潔。
YML檔案的擴充名可以是,yml和.yaml。
-
yml配置檔案的文法
-
配置普通資料
- 文法:key: value
- 示例代碼
-
name: zhangsan
- 注意:value之前有一個空格
-
配置對象資料
- 文法:
- key:
- key1:value1
- key2:value2
- 或者:
- key:{key1:value1,key2:value2}
- 執行個體代碼:
注意:key1前面的空格個數不限,在yml文法中,相同縮進代表同一個級别person: name: zhangsan age: 23 address: zhengzhou #或者 person: {name: zhangsan,age: 23,address: zhengzhou}
-
配置map資料
與上面的配置對象資料相同 -
配置數組(List、Set)資料
- 文法:
- key:
-
- value1
- -value2
- 或者:
- key:[value1,value2]
- 執行個體代碼:
注意:-後面要有空格city: - beijing - changsha - zhengzhou - shanghai #或者 city: [beijing,changsha,zhengzhou,shanghai] #配置對象數組 student: - name: tom age: 18 address: beijing - name: rabbit age: 17 address: zhenghzou #行内樣式配置對象數組 student: [{name: tom,age: 18,address: beijing},{name: rabbit,age: 17,address: zhengzhou}]
-
二、配置檔案與配置類的屬性映射
1.使用注解@Value映射
我們可以使用@Value注解将配置檔案中的值映射到一個Spring管理的Bean的字段上
例如:
application.properties配置如下:
name=wangwu
student.name=zhaoliu
student.age=66
student.address=shijiazhuang
application.yml配置如下:
name: 張三
student:
name: 李四
age: 24
address: 鄭州
測試類代碼如下:
package com.test.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Student {
@Value("${name}")
private String name;
@Value("${student.address}")
private String address;
@ResponseBody
@RequestMapping("/test")
public String test() {
return "name:"+name+",address:"+address;
}
}
該種方式的優缺點:
優點:比對字段更加精确。
缺點:如果字段過多需要每個屬性都設定@Value注解
2.使用注解@ConfigurationProperties映射
通過注解@ConfigurationProperties(prefix=“配置檔案種key的字首”)可以将配置檔案中的配置自動與實體進行映射
appication.properties配置如下:
name=wangwu
student.name=zhaoliu
student.age=66
student.address=shijiazhuang
application.yml配置如下:
name: 張三
student:
name: 李四
age: 24
address: 鄭州
測試類代碼如下
package com.test.controller;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ConfigurationProperties("student")
public class Student1 {
private String name;
private String address;
@ResponseBody
@RequestMapping("/test1")
public String test1() {
return "name:"+name+",address:"+address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
這裡我們會發現編譯器會有警告,沒有添加@ConfigurationProperties的執行器。(雖然沒有配置也能成功),配置完成之後,在properties檔案或者yml檔案中會出現對應的提示。配置在pom.xml檔案中
代碼如下:
<!-- @ConfigurationProperties執行器的配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>