配置文件佔位符
- 隨機數
${random.value}/${random.int}、${ramdom.long}
- 佔位符獲取之前配置的值,如果沒有可以使用冒號指定默認值
${person.hello:abc}
person沒有hello的屬性,冒號指定默認值,最後輸出abc。
Profile
Profile是Spring對不同環境提供不同配置功能的支持,可以通過激活、指定參數等方式快速切換環境。
- 多Profile文件
在主配置文件編寫的時候,文件名可以是 application-{profile}.properties/yml
就可以動態進行切換
默認使用application.properties; - yml支持多文檔塊模式
server:
port: 8081
spring:
profiles:
active: prod
---
server:
port: 8082
spring:
profiles: dev
---
server:
port: 8083
spring:
profiles: prod
- 激活指定profile
- 在配置文件中指定:在配置文件中使用spring.profiles.active=xxx可以激活指定profile
- 命令行:
-spring.profiles.active=dev(運行jar包的時候,可以直接在測試的時候,配置傳入命令行參數)優先級高於1; - 在編譯器Program arguments中指定:—apring.profiles.active=xxx環境;並且這個優先級高於1;
- 虛擬機參數:編譯器VM options:-Dspring.profile.active=xxx環境;
配置文件加載位置:
springboot啓動會掃描以下位置的application.properties或者application.yml文件作爲SpringBoot的默認配置。