SpringBoot外部配置

官方文档: Spring Boot Reference Documentation

老文档 :25. Externalized Configuration

加载顺序

  1. 开启 DevTools 时,~/.spring-boot-devtools.properties
  2. 测试类上的 @TestPropertySource 注解
  3. @SpringBootTest#properties 属性
  4. 命令⾏参数( --server.port=9000
  5. SPRING_APPLICATION_JSON 中的属性
  6. ServletConfig 初始化参数
  7. ServletContext 初始化参数
  8. java:comp/env 中的 JNDI 属性
  9. System.getProperties()
    通过 java -D 指定的参数
  10. 操作系统环境变量
  11. random.* 涉及到的 RandomValuePropertySource
    可以获取随机值
    profile会比不加profile的优先
  12. jar 包外部的 application-{profile}.properties 或 .yml
  13. jar 包内部的 application-{profile}.properties 或 .yml
  14. jar 包外部的 application.properties 或 .yml
  15. jar 包内部的 application.properties 或 .yml
  16.  @Configuration 类上的 @PropertySource
  17. SpringApplication.setDefaultProperties() 设置的默认属性

application.properties可放的位置

  • ./config    当前路径
  • ./          当前路径
  • CLASSPATH  /config    classpth中
  • CLASSPATH  /               classpth中

修改配置文件名称和位置

在org.springframework.boot.context.config.ConfigFileApplicationListener中

  • spring.config.name
  • spring.config.location
  • spring.config.additional-location

SpringBoot对Relaxed Binding的支持

   Spring Boot使用一些relaxed rules在@ConfigurationProperties bean上绑定环境配置,所以环境属性名和bean属性名不需要精确匹配.

Property

Note

acme.my-project.person.first-name

横线规则Kebab case, which is recommended for use in .properties and .yml files.

acme.myProject.person.firstName

标准驼峰 Standard camel case syntax.

acme.my_project.person.first_name

下划线Underscore notation, which is an alternative format for use in .properties and .yml files.

ACME_MYPROJECT_PERSON_FIRSTNAME

全大写下划线Upper case format, which is recommended when using system environment variables.

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FantasyBaby

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值