• springboot-配置文件优先级


    官方文档 https://docs.spring.io/spring-boot/docs/2.7.16/reference/htmlsingle/#features.external-config

    Spring Boot允许外部化配置,这样就可以在不同的环境中使用相同的应用程序代码。您可以使用各种外部配置源,包括Java属性文件、YAML文件、环境变量和命令行参数

    属性值可以使用@Value注解直接注入到bean中,通过Spring的环境抽象访问,也可以通过@ConfigurationProperties绑定到结构化对象

    外部配置优先级

    Spring Boot使用了一个非常特殊的PropertySource顺序,旨在允许合理的值覆盖。后面的属性源可以覆盖前面定义的值。来源按以下顺序考虑(优先级从小到大):

    1. Default properties (specified by setting SpringApplication.setDefaultProperties).
    2. @PropertySource annotations on your @Configuration classes.
      • 请注意,在刷新应用程序上下文之前,此类属性源不会添加到环境中。
      • 这对于logging.* spring.main.*在刷新开始之前就要set的配置来说:太晚了
    3. Config data (such as application.properties files).
    4. A RandomValuePropertySource that has properties only in random.*.
    5. OS environment variables.
    6. Java System properties (System.getProperties()). ; VM options,使用java -Dage=18
    7. JNDI attributes from java:comp/env.
    8. ServletContext init parameters.
    9. ServletConfig init parameters.
    10. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
    11. Command line arguments.
    12. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
    13. @DynamicPropertySource annotations in your tests.
    14. @TestPropertySource annotations on your tests.
    15. Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.

    在这里插入图片描述
    最终转换的执行命令: java -Dage=22 -classpath **.jar cn.jhs.spring.boot.App --name=joe
    执行结果name=joe,age=22,sex=M

    Application Properties优先级

    https://docs.spring.io/spring-boot/docs/2.7.16/reference/htmlsingle/#features.external-config.files

    springboot默认会加载application.yaml或application.properties中的配置信息.
    !!!注意: 同名文件,".properties"优先级大于".yaml"

    1.application.properties and YAML

    1.1 优先级

    在这里插入图片描述

    案例说明:
    在这里插入图片描述

    1.2 spring.config.name

    如果你不喜欢用application作为配置文件名,可以通过指定spring.config.name环境属性来切换到另一个文件名。

    # myproject.properties/yaml
    java -jar myproject.jar --spring.config.name=myproject
    
    • 1
    • 2
    1.3spring.config.location

    还可以使用spring.config.location环境属性来引用显式位置。此属性接受一个逗号分隔的列表,其中包含要检查的一个或多个位置。

    # 若default.properties与override.properties存在同名的配置,后面的会覆盖前面的; **last-wins**策略
    $ java -jar myproject.jar --spring.config.location=\
        optional:classpath:/default.properties,\
        optional:classpath:/override.properties
    
    • 1
    • 2
    • 3
    • 4
    1.4 spring.config.additional-location
    #1. 设置 spring.config.additional-location
    spring.config.additional-location=optional:classpath:/custom-config/,optional:file:./custom-config/
    
    #2.优先级(优先级从小到大,后面覆盖前面)
    ## additional-location高于spring.config.location优先级
    optional:classpath:/;optional:classpath:/config/
    optional:file:./;optional:file:./config/;optional:file:./config/*/
    optional:classpath:custom-config/
    optional:file:./custom-config/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.application-{profile}.properties and YAML

    https://docs.spring.io/spring-boot/docs/2.7.16/reference/htmlsingle/#features.external-config.files.profile-specific

    Spring Boot还会尝试使用命名约定application-{profile}来加载特定于profile的文件。例如,如果您的应用程序激活一个名为prod的配置文件,那么application.yaml 和 application-prod.yaml 都会被使用。

    特定于profile的文件总是覆盖非特定的配置。
    如果指定了多个profile,则采用last-wins策略。例如,如果spring.profiles.active设置为prod,live由属性指定,值在application-prod.properties属性可以被application-live.properties中的属性覆盖。

    2.1 last-win 综合案例
    #1. profiles : prod,live
    
    #2. 存在配置文件
    /cfg
      application-live.properties
    /ext
      application-live.properties
      application-prod.properties
    
    # 3.1 若 spring.config.location=classpath:/cfg/,classpath:/ext/ ,优先级(后面的覆盖前面)
    # /cfg 总是会被 /ext 覆盖
    /cfg/application-live.properties
    /ext/application-prod.properties
    /ext/application-live.properties
    
    # 3.2 若 spring.config.location=classpath:/cfg/;classpath:/ext/    (注意是; 不是,)
    # # /cfg 和/ext 优先级一致
    /ext/application-prod.properties
    /cfg/application-live.properties
    /ext/application-live.properties
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    3. jar包外的 application.properties and YAML

    4 jar包外的application-{profile}.properties and YAML



    其它

    yaml分隔符

    yaml可以使用---分割不同的配置块,当spring.profiles.active=dev时,配置文件优先级:
    在这里插入图片描述
    此时的environment.getPropertySources().propertySourceList
    在这里插入图片描述

    @PropertySource(value={xx,yy}) 多个配置

    @Component
    @ConfigurationProperties(prefix = "my.cache")
    //注意:默认仅支持 *.properties
    @PropertySource(value = {"classpath:cache.properties","file:./config/cache.properties"}, ignoreResourceNotFound = true)
    public class CacheConfigProperties {
        private String name;
        private long timeout;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    这里@PropertySource引入的资源,跟code顺序无关, 同样按照 file > classpath的优先级来加载。

    所以我们可以在project中填写默认的配置。
    部署服务器时,在file目录下,填写实际的配置。

    宽松的绑定规则

    https://docs.spring.io/spring-boot/docs/2.7.16/reference/htmlsingle/#features.external-config.typesafe-configuration-properties.relaxed-binding

    Spring Boot使用了一些宽松的规则将环境属性绑定到@ConfigurationProperties bean上,因此环境属性名和bean属性名不需要完全匹配

    不支持@Value(), @Value必须严格的匹配

    @ConfigurationProperties class:
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "my.main-project.person")
    public class MyPersonProperties {
    
        private String firstName;
    
        public String getFirstName() {
            return this.firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在上述代码中,可以使用下列属性名)

    propertyNode
    my.main-project.person.first-name -.properties and YAML 使用
    my.main-project.person.firstName 标准的驼峰标识.
    my.main-project.person.first_name _.properties and YAML 使用
    MY_MAINPROJECT_PERSON_FIRSTNAME 大写格式,仅在system environment variables 才有效

    注解前缀必须都是小写字符或者用-分隔
    优先级:大写格式 > ‘-’ > '_' > 驼峰
    测试

    #1. 结果为first-name
    MY_MAINPROJECT_PERSON_FIRSTNAME: MY_MAINPROJECT_PERSON_FIRSTNAME
    my:
      main-project:
        person:
          firstName: firstName
          first_name: first_name
          first-name: first-name
    
    #2. 结果为 firstName
    MY_MAINPROJECT_PERSON_FIRSTNAME: MY_MAINPROJECT_PERSON_FIRSTNAME
    my:
      main-project:
        person:
          firstName: firstName
          first_name: first_name
    #      first-name: first-name
    
    #3.结果为 first_name
    MY_MAINPROJECT_PERSON_FIRSTNAME: MY_MAINPROJECT_PERSON_FIRSTNAME
    my:
      main-project:
        person:
    #      firstName: firstName
           first_name: first_name
    #      first-name: first-name
    
    #4.结果为 --- 报错null (说明该方式只支持在system environment)
    MY_MAINPROJECT_PERSON_FIRSTNAME: MY_MAINPROJECT_PERSON_FIRSTNAME
    my:
      main-project:
        person:
    #      firstName: firstName
    #       first_name: first_name
    #      first-name: first-name
    
    #5. 结果为:MY_MAINPROJECT_PERSON_FIRSTNAME
    ## 5.1 system environment
    MY_MAINPROJECT_PERSON_FIRSTNAME=MY_MAINPROJECT_PERSON_FIRSTNAME
    ## 5.2 properties文件
    my:
      main-project:
        person:
          firstName: firstName
          first_name: first_name
          first-name: first-name
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    @ConfigurationProperties vs. @Value

    在这里插入图片描述

    1. Relaxed binding
      • @Value("${demo.item-price}")
        • 可以使用 application.properties文件中的demo.item-pricedemo.itemPrice
        • 以及系统变量中的DEMO_ITEMPRICE
      • @Value("${demo.itemPrice}"): 无法使用 demo.item-priceDEMO_ITEMPRICE
      • 结论: @Value推荐使用aa-bb-cc格式配置
    2. Meta-data support

    Configuration metadata files位于META-INF/spring-configuration-metadata.json下的jar包中。它们使用JSON格式,将项目分类为“groups”或“properties”,其他值分类为“hints”,如下面的例子所示:
    通过如下配置,会告诉你如下属性在哪里被使用。在这里插入图片描述

  • 相关阅读:
    windows11 下使用 modelscope docker环境 实现 离线语音转文字
    创建一个链表
    【BOOST C++ 10 时间数据】(2)本地独立时间
    [Elastic-Job2.1.5源码]-12-调度作业的服务器IP和进程信息的持久化是如何设计的?
    Java并发编程学习12-任务取消(上)
    Elasticsearch第二篇:es之间版本比较
    python(10)
    【代码随想录】算法训练计划15
    TCP 超时重传机制
    js将后端返回的blob请求下载
  • 原文地址:https://blog.csdn.net/it_freshman/article/details/125004785