spring boot學習(6)— 配置資訊及其讀取優先順序

工號1024發表於2019-03-30

1. properties 資訊從哪裡取

在不同的環境,我們需要使用不同的配置,Spring boot 已經提供了相關功能,可以是 properties 檔案, yaml 檔案 或是命令列引數。優先順序如下

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).

  2. @TestPropertySource annotations on your tests.

  3. @SpringBootTest#properties annotation attribute on your tests.

  4. Command line arguments.

    java -jar app.jar --name="Spring"
    複製程式碼
  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).

    • environment vaiable:SPRING_APPLICATION_JSON='{"acme":{"name":"test"}}' java -jar myapp.jar
    • command line:
      • java -Dspring.application.json='{"name":"test"}' -jar myapp.jar
      • java -jar myapp.jar --spring.application.json='{"name":"test"}'
  6. ServletConfig init parameters.

  7. ServletContext init parameters.

  8. JNDI attributes from java:comp/env.

  9. Java System properties (System.getProperties()).

  10. OS environment variables.

  11. 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]}
複製程式碼
  1. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
  2. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  3. Application properties outside of your packaged jar (application.properties and YAML variants).
  4. Application properties packaged inside your jar (application.properties and YAML variants).
  5. @PropertySource annotations on your @Configuration classes.
  6. Default properties (specified by setting SpringApplication.setDefaultProperties).

2. 使用 application.properties 檔案

使用 properties 檔案,spring boot 會根據以下目錄去尋找,新增到 Spring Environment 中,優先順序依次遞增。

  1. classpath:/: resources 目錄
  2. classpath:/config/: resourcesconfig 目錄
  3. file:./:工程根目錄
  4. file:./config/: 工程跟目錄下的 config 目錄

2.1 載入順序:

從優先順序高的先載入。

  1. file:./config/
  2. file:./
  3. classpath:/config/
  4. classpath:/
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)
複製程式碼

2.2 屬性值怎麼取

優先順序高的會覆蓋優先順序低的。

  1. ./config/application.properties
    testconfig.first=./config/
    #testconfig.second=./config/
    #testconfig.third=./config/
    #testconfig.fourth=./config/
    複製程式碼
  2. ./application.properties
    testconfig.first=./
    testconfig.second=./
    #testconfig.third=./
    #testconfig.fourth=./
    複製程式碼
  3. classpath:/config/application.properties
    testconfig.first=classpath/config/
    testconfig.second=classpath/config/
    testconfig.third=classpath/config/
    #testconfig.fourth=classpath/config/
    複製程式碼
  4. 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)

複製程式碼

2.3 多環境配置檔案

加一個檔案: 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/
複製程式碼

2.3 使用 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/
複製程式碼

2.5 屬性檔案中可以使用變數已經宣告過的變數值:

app.name=MyApp
app.description=${app.name} is a Spring Boot application
複製程式碼

相關文章