配置文件的作用
整個項目中所有重要的數據都是在配置文件中配置,如數據庫的連接信息,項目的啟動端口,用于發現和定位問題的普通日志和異常日志等等。配置文件可以分為兩類
- 系統使用的配置文件(系統配置文件),如端口號的設置,連接數據庫的配置
- 用戶自定義的配置文件
配置文件的格式
Spring Boot的配置文件可以分為 .properties和 .yml兩種格式。.properties屬于早期時代的格式,也是Spring Boot項目的默認配置文件。當一個項目中存在兩種格式的配置文件,并且兩個配置文件都設置了相同的配置項,但值不同,那么properties的優先級更高。通常在一個項目中只會存在一種格式的配置文件。
properties的用法
- properties是以鍵值的形式配置的,key和value之間用“=”連接,中間沒有空格。
# 系統配置端口號
server.port=8888
# 自定義配置
name=zhangsan
- 使用@Value注解使用${}的格式讀取配置文件
@RestControllerpublic class Controller {@Value("${name}")//要和配置文件中的key值相同private String name;@PostConstructpublic void sayHi() {System.out.println("hi: " + name);}}
:::info
- propertices的缺點
:::
我們發現properties有很多冗余信息,而使用yml就可以很好的解決
yml配置文件
yml的優點
- yml寫法簡單,可讀性高
- yml支持更多數據類型,如數組,對象
- yml支持多語言
稍微規模大點的公司都開始使用微服務,像字節內部有java,go,python,php語言,只關心業務是否能夠實現,使用什么語言并不關心。如果使用properties配置文件就要寫多份,而yml就很好的解決了這個問題。
key: value
注意:key和value之間使用冒號和空格組成
yml版本的連接數據庫
yml進階
配置不同數據類型
#字符串
string.value: hello
#布爾值
boolean.value: true
boolean.value2: false
#整數
int.value: 10
#浮點數
float.value: 3.14
#空值,~表示Null
null.value: ~
讀取配置文件中的基礎類型使用@Value(“${}”)注解
@RestControllerpublic class Controller {//要和key值對應@Value("${string.value}")private String hello;@PostConstructpublic void postConstruct() {System.out.println(hello);}@Value("${boolean.value}")private boolean bool;@PostConstructpublic void postConstruct2() {System.out.println(bool);}@Value("${null.value}")private Integer integer;@PostConstructpublic void postConstruct3() {System.out.println(integer);}}
:::success
注意:value值加單/雙引號
:::
string:str1: hello \n Spring Bootstr2: 'hello \n Spring Boot'str3: "hello \n Spring Boot"
@RestController
public class Controller {@Value("${string.str1}")private String str1;@PostConstructpublic void construct1() {System.out.println("str1: " + str1);}@Value("${string.str2}")private String str2;@PostConstructpublic void construct2() {System.out.println("str2: " + str2);}@Value("${string.str3}")private String str3;@PostConstructpublic void construct3() {System.out.println("str3: " + str3);}
}
由上可知
- 字符串默認不用加上單引號或者雙引號
- 單引號會轉義特殊字符,特殊字符最終只是一個普通的字符串數據
- 雙引號不會轉義字符串里面的特殊字符,特殊字符會作為本身想表示的意思
配置對象
#自定義一個對象,兩種寫法
#student:
# id: 1
# name: zhangsan
# age: 18
student: {id: 1, name: zhangsan, age: 18}
讀取配置文件中的對象使用@ConfigurationProperties注解
@Component
//三種寫法
//@ConfigurationProperties(value = "student")
//@ConfigurationProperties(prefix = "student")
@ConfigurationProperties("student")
@Data //需要提供get,set方法才能夠把配置文件的信息讀取出來
public class Student {//類型和名字要一一對應private int id;private String name;private int age;
}
配置集合
#自定義集合
#list:
# array:
# - 1
# - 2
# - 3
list: {array: [1,2,3]}
讀取配置文件中的集合使用@ConfigurationProperties
@Component@ConfigurationProperties("list")@Datapublic class MyList {private List<Integer> array;}
properties和yml的區別
- properties是以key=value的形式進行配置,而yml是使用類json格式。
- properties為早期且默認的配置文件格式,其配置存在一定的冗余數據,使用yml可以很好的解決數據冗余的問題
- yml通用性更好,支持多種語言,例如開發一個云服務器,可以使用同一份配置文件作為java和go的共同配置文件
- yml支持更多的數據類型
設置不同環境的配置文件
在一個項目中有多種環境,如開發環境,測試環境,生產環境。每個環境的配置項都有所不同,如何讓一個配置文件適應不同的環境呢?
把配置文件設為生產環境
spring:profiles:active: prod
server:port: 9999
server:port: 7777
此時使用的就是生產環境配置的端口號