3個配置文件: application.properties
application-dev.properties
application-prod.properties
java
第1個: application.properties
spring
server.port=8888 spring.profiles.active=dev person.firstName=新名 person.lastName=${random.uuid} person.age=${random.int(100)} person.male=true person.birth=1985/03/03 person.maps.k1=v1 person.maps.k2=20 person.maps.k3=true person.lists=lisi,wangwu person.dog.name=${person.firstName:無名}_小d person.dog.age=${random.int[30,40]}
第2個: application-dev.properties
json
server.port=9999 person.lastName=${random.uuid} person.age=${random.int(100)} person.male=true person.birth=1985/03/03 person.maps.k1=v1 person.maps.k2=20 person.maps.k3=true person.lists=lisi,wangwu person.dog.name=${person.firstName:無名dev}_小dev #person.dog.age=${random.int[10,20]}
第3個: application-prod.properties
segmentfault
server.port=7777 person.lastName=${random.uuid} person.age=${random.int(100)} person.male=true person.birth=1985/03/03 person.maps.k1=v1 person.maps.k2=20 person.maps.k3=true person.lists=lisi,wangwu person.dog.name=${person.firstName:無名prod}_小prod person.dog.age=${random.int[20,30]}
應用啓動的時候, 先去 第1個 配置文件 application.properties中找 spring.profiles.active=dev
找到 dev
這樣, 就會讀取application-dev.properties 的配置文件來加載; 那麼問題來了:springboot
- 先加載 application.properties中的配置
- 而後加載application-dev.properties中的配置, 若是重複的, 用dev去覆蓋上面的; 若是dev中沒有的, application.properties中的配置就生效!
- 回答如上: 會生效, 除非dev中的覆蓋了;
上面三個配置文件設置後, person對象的屬性加載結果是: 可見 person.dog.age
被application.properties的起做用了;app
{"lastName":"94b68ef1-33d2-4866-9b62-32c5fb983f22","age":47,"male":true,"birth":"Mar 3, 1985 12:00:00 AM","maps":{"k1":"v1","k2":"20","k3":"true"},"lists":["lisi","wangwu"],"dog":{"name":"新名_小dev","age":32}}
spring: profiles: active: prod person: firstName: newName --- spring: profiles: dev server: port: 5555 person: lastName: ${random.uuid} # uuid age: ${random.int(100)} # 0-100的隨機值 male: true birth: 1985/03/03 maps: {k1: v1, k2: 20, k3: true} lists: - lisi - wangwu dog: name: ${person.firstName:無名}_小黃dev #若是爲空, 取默認值 age: ${random.int[1,3]} #10-20之間的隨機值: 中間還不能有空格, 實測! --- spring: profiles: prod server: port: 6666 person: lastName: ${random.uuid} # uuid age: ${random.int(100)} # 0-100的隨機值 male: true birth: 1985/03/03 maps: {k1: v1, k2: 20, k3: true} lists: - lisi - wangwu dog: name: ${person.firstName:無名}_小黃prod #若是爲空, 取默認值 age: ${random.int[4,6]} #10-20之間的隨機值: 中間還不能有空格, 實測!
---
能夠做爲文檔分割符號dom
問題來了:ui
- 兩個都會讀取;
- application.yml先於application.properties加載; (示例證實以下)
- 因爲加載有前後關係, properties的屬性值會覆蓋yml中的;
在上面 application.yml
的基礎上, 若是application.properties
中也有配置:3d
person.firstName=新名
則最終結果是:code
{"lastName":"6d3a8f32-7736-48c8-9fb9-9fb3f73da6b7","age":36,"male":true,"birth":"Mar 3, 1985 12:00:00 AM","maps":{"k1":"v1","k2":20,"k3":true},"lists":["lisi","wangwu"],"dog":{"name":"新名_小黃prod","age":4}}
說明了上面的結論: application.properties
中的
person.firstName=新名
覆蓋了 application.yml
中的 person.firstName: newName
; 同時也證實兩個都會加載!
會覆蓋項目中的配置:
java -jar xxx.jar -Dspring.profiles.active=prod