properties
信息從哪裏取在不一樣的環境,咱們須要使用不一樣的配置,Spring boot 已經提供了相關功能,能夠是 properties
文件, yaml
文件 或是命令行參數。優先級以下java
Devtools global settings properties
on your home directory (~/.spring-boot-devtools.properties
when devtools is active).spring
@TestPropertySource
annotations on your tests.json
@SpringBootTest#properties
annotation attribute on your tests.bash
Command line arguments.app
java -jar app.jar --name="Spring"
複製代碼
Properties from SPRING_APPLICATION_JSON
(inline JSON embedded in an environment variable or system property).less
SPRING_APPLICATION_JSON='{"acme":{"name":"test"}}' java -jar myapp.jar
java -Dspring.application.json='{"name":"test"}' -jar myapp.jar
java -jar myapp.jar --spring.application.json='{"name":"test"}'
ServletConfig
init parameters.dom
ServletContext
init parameters.ide
JNDI attributes from java:comp/env
.spring-boot
Java System properties (System.getProperties()
).ui
OS environment variables.
A RandomValuePropertySource that has properties only in random.*.
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}
複製代碼
application.properties
文件使用 properties
文件,spring boot 會根據如下目錄去尋找,添加到 Spring Environment
中,優先級依次遞增。
classpath:/
: resources
目錄classpath:/config/
: resources
下 config
目錄file:./
:工程根目錄file:./config/
: 工程跟目錄下的 config
目錄從優先級高的先加載。
2019-03-27 22:38:24.848 DEBUG 39802 --- [ main] o.s.boot.SpringApplication : Loading source class com.example.exitcode.DemoApplication
2019-03-27 22:38:24.915 DEBUG 39802 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'file:./config/application.properties' (file:./config/application.properties)
2019-03-27 22:38:24.915 DEBUG 39802 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'file:./application.properties' (file:./application.properties)
2019-03-27 22:38:24.915 DEBUG 39802 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'jar:file:xxxxx-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/config/application.properties' (classpath:/config/application.properties)
2019-03-27 22:38:24.915 DEBUG 39802 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'jar:file:xxxxx-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/application.properties' (classpath:/application.properties)
複製代碼
優先級高的會覆蓋優先級低的。
./config/application.properties
testconfig.first=./config/
#testconfig.second=./config/
#testconfig.third=./config/
#testconfig.fourth=./config/
複製代碼
./application.properties
testconfig.first=./
testconfig.second=./
#testconfig.third=./
#testconfig.fourth=./
複製代碼
classpath:/config/application.properties
testconfig.first=classpath/config/
testconfig.second=classpath/config/
testconfig.third=classpath/config/
#testconfig.fourth=classpath/config/
複製代碼
classpath:/application.properties
testconfig.first=classpath
testconfig.second=classpath
testconfig.third=classpath
testconfig.fourth=classpath
複製代碼
輸出以下:
2019-03-27 23:29:12.434 INFO 1335 --- [ main] com.example.properties.DemoApplication : No active profile set, falling back to default profiles: default
first: ./config/
second: ./
third: classpath/config/
fourth: classpath
2019-03-27 23:29:13.052 INFO 1335 --- [ main] com.example.properties.DemoApplication : Started DemoApplication in 16.565 seconds (JVM running for 23.467)
複製代碼
加一個文件: classpath:/application-product.properties
testconfig.first=product-classpath
testconfig.second=product-classpath
複製代碼
經過 spring.profiles.active
來指定環境所對應的 properties
文件: 運行 java -jar build/libs/properties-0.0.1-SNAPSHOT.jar --spring.profiles.active=product
, 輸出以下:
2019-03-28 20:34:44.726 INFO 25859 --- [ main] com.example.properties.DemoApplication : The following profiles are active: product
first: product-classpath
second: product-classpath
third: classpath/config/
fourth: classpath
fifth: ./config/
sixth: ./config/
seventh: ./config/
eightth: ./config/
複製代碼
yaml
文件來代替 properties
文件。也能夠使用 yaml
格式的文件。可是在同等目錄下,properties
優先級高於 yaml
文件的配置信息。
新增文件 ./config/application.yml
testconfig:
frist: ./config/yml
second: ./config/yml
複製代碼
命令 java -jar build/libs/properties-0.0.1-SNAPSHOT.jar
輸出爲:
first: ./config/
second: ./config/yml
third: classpath/config/
fourth: classpath
fifth: ./config/
sixth: ./config/
seventh: ./config/
eightth: ./config/
複製代碼
app.name=MyApp
app.description=${app.name} is a Spring Boot application
複製代碼