• springboot配置文件加载顺序


    一、背景

    SpringBoot的配置文件可以给环境变量中的指定key设置value,程序运行时候,从环境中通过key获得value,从而可以实现通过改变配置文件的值,控制程序运行效果,实现业务逻辑。

    二、配置文件的位置

    1. ./
    2. ./config/
    3. ./config/*/
    4. resources/
    5. resources/config/

    三、配置文件的类型

    1. application.properties
    2. application.yml
    3. application.yaml

    四、示例代码

    springboot-config-load-order

    五、演示结果

    • 优先级高低的含义
      对于key相同的配置项,优先级高的配置文件会覆盖掉优先级低的配置文件,
      优先级低的配置文件,如果想要配置项起作用,只能采用不同的key

    SpringBoot版本:>=2.4.2(截止文章发布,最高版本2.7.5)

    位置优先级:(依次降低)
    1> ./config/*/
    2> ./config/
    3> ./
    4> Resources/config/
    5> Resorces/
    文件类型优先级:(依次降低)
    1> .properties
    2> .yml
    3> .yaml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    SpringBoot版本:2.4.1

    位置优先级:(依次降低)
    1> ./config/*/
    2> ./config/
    3> ./
    4> Resources/config/
    5> Resorces/
    文件类型优先级:(依次降低)
    1> .yaml
    2> .yml
    3> .properties
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    SpringBoot版本:2.4.0

    位置优先级:(依次降低)
    1> ./config/
    2> ./config/*/
    3> ./
    4> Resources/config/
    5> Resorces/
    文件类型优先级:(依次降低)
    1> .yaml
    2> .yml
    3> .properties
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    SpringBoot版本:2.3.[0,12].RELEASE

    位置优先级:(依次降低)
    1> ./config/
    2> ./config/*/
    3> ./
    4> Resources/config/
    5> Resorces/
    文件类型优先级:(依次降低)
    1> .properties
    2> .yml
    3> .yaml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    SpringBoot版本:2.2.x.RELEASE

    位置优先级:(依次降低)
    1> ./config/
    2> 不会去加载./config/*/下的任何配置文件
    3> ./
    4> Resources/config/
    5> Resorces/
    文件类型优先级:(依次降低)
    1> .properties
    2> .yml
    3> .yaml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    再原始的版本省略,感兴趣自行测试.

    六、特殊情况

    指定优先级配置:spring.profiles.active=xxx,该配置项服从以下规则:

    1. 该配置也是一个配置项,起作用的优先级服从上述规则
    2. 最高优先级的会覆盖掉低优先级的(注意不是合并而是覆盖)
    3. xxx可以指定多个配置文件,例如dev,alpha,prod,越靠后的优先级越高
    4. 被指定的配置文件xxx中不可以再重复出现spring.profiles.active=yyy的配置
    5. 优先级高的配置项才会起作用
  • 相关阅读:
    【Python】使用 Matplotlib 和 Numpy 的 知识绘制一个简单的 二次函数图像 || 同一坐标系下绘制多个函数图像
    Vue3.0项目——打造企业级音乐App(二)图片懒加载、v-loading指令的开发和优化
    Java判断考试成绩程序代码:
    【在线教育】EasyExcel入门
    math_高数公式每日一过_part2(private)
    python自动解析301、302重定向链接
    SQL 游标
    【JavaScript】ES6 中class定义类
    【C++庖丁解牛】类与对象
    系列五、线程间通信
  • 原文地址:https://blog.csdn.net/qq_33547321/article/details/127769868