spring boot学习(6)— 配置信息及其读取优先级

2019/12/05 10:00
阅读数 142

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
复制代码

 

展开阅读全文
打赏
0
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部