Spring Boot 配置文件

在配置文件的隨機方法java

#隨機字符串
com.forwy.value=${random.value}

#隨機 int
com.forwy.int=${random.int}

#隨機 long
com.forwy.long=${random.long}

#隨機 int (10之內)
com.forwy.int=${random.int(10)}

#隨機 int (10~20)
com.forwy.int=${random.int[10,20]}

多環境配置:spring

多環境配置文件須要知足 application-{profile}.properties 的命名格式,其中 {profile} 爲環境標識符,如 dev ,prod 等
application-dev.properties
application-test.properties
application-prod.properties

Spring Boot 根據 application.properties 中的 spring.profiles.active 屬性來加載配置文件,如app

spring.profiles.active=dev

則默認加載 application-dev.properties 配置文件。
在部署到其餘環境時,則在啓動命令上重寫這個屬性,好比部署到 prod 環境上dom

java -jar xxx.jar --spring.profiles.action=prod

則將加載 application-prod.properties 配置文件。(命令傳入的參數在Spring Boot 的屬性加載優先級最高)
Spring Boot 的屬性加載優先級:命令行

命令行傳入的參數
.
Spring_Application_JSON 中的屬性
.
java:comp/env 中的 JNDI 屬性
.
java的系統屬性
.
系統環境變量
.
random.*配置內容
.
位於 jar 包以外,針對不一樣環境配置的配置文件,如 application-dev.properties 。
.
使用 @Configuration 註解的類中,經過 @PropertySource 註解定義的屬性。
.
應用默認屬性,使用 SpringApplication.setDefaultProperties 定義的內容。
相關文章
相關標籤/搜索