• SpringBoot配置文件加载顺序


    Spring Boot 允许您外部化 configuration,以便您可以在不同的环境中使用相同的 application code。您可以使用 properties files,YAML files,环境变量和 command-line arguments 来外部化 configuration。 Property 值可以通过使用@Value annotation 直接注入 beans,通过 Spring 的Environment抽象访问,或者绑定到结构化的 objects@ConfigurationProperties

    Spring Boot 使用一个非常特殊的PropertySource order 来设计允许合理地覆盖值。 Properties 在以下 order 中被考虑:

    • 您的主目录上的Devtools global 设置 properties(当 devtools 为 active 时为~/.spring-boot-devtools.properties)。

    • @TestPropertySource 注释测试。

    • 你测试的properties属性。可在@SpringBootTesttest annotations 用于测试 application 的特定切片上使用。

    • 命令 line arguments。

    • _Pro来自SPRING_APPLICATION_JSON(嵌入在环境变量或系统 property 中的内联 JSON)。

    • ServletConfig init 参数。

    • ServletContext init 参数。

    • 来自java:comp/env的 JNDI 属性。

    • Java System properties(System.getProperties())。

    • OS 环境变量。

    • 只在random.*中具有 properties。

    • jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件

    • jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件

    • jar包外部的application.properties或application.yml(不带spring.profile)配置文件

    • jar包内部的application.properties或application.yml(不带spring.profile)配置文件

    • @Configuration classes 上的@PropertySource 注释。

    • 默认 properties(通过设置SpringApplication.setDefaultProperties指定)。

    要提供具体的 example,假设您开发了一个使用name property 的@Component,如下面的示例所示:

    import org.springframework.stereotype.*;
    import org.springframework.beans.factory.annotation.*;
    ​
    @Component
    public class MyBean {
    ​
        @Value("${name}")
        private String name;
    ​
        // ...
    ​
    }

    在 application classpath(对于 example,在 jar 中),您可以拥有一个application.properties文件,为name提供合理的默认 property value。在新环境中运行时,可以在 jar 之外提供覆盖nameapplication.properties文件。对于 one-off 测试,您可以使用特定命令 line 开关启动(对于 example,java -jar app.jar --name="Spring")。

    可以在命令 line 上使用环境变量提供SPRING_APPLICATION_JSON properties。对于 example,您可以在 UN * X shell 中使用以下 line:

    $ SPRING_APPLICATION_JSON='{"acme":{"name":"test"}}' java -jar myapp.jar

    在前面的 example 中,最终在 Spring Environment中得到了acme.name=test。您还可以在 System property 中将 JSON 提供为spring.application.json,如以下 example 所示:

    $ java -Dspring.application.json='{"name":"test"}' -jar myapp.jar

    您还可以使用命令 line 参数提供 JSON,如以下 example 所示:

    $ java -jar myapp.jar --spring.application.json='{"name":"test"}'

    您还可以将 JSON 作为 JNDI 变量提供,如下所示:java:comp/env/spring.application.json

    配置随机值

    RandomValuePropertySource对于注入随机值非常有用(例如,对于秘密或测试用例)。它可以生成整数,长整数,uuids 或 strings,如下面的示例所示:

    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]}

    random.int*语法是OPEN value (,max) CLOSE,其中OPEN,CLOSE是任何字符,value,max是整数。如果提供max,则value是最小值,max是最大 value(不包括)。

    访问命令 Line Properties

    默认情况下,SpringApplication将任何命令 line 选项 arguments(即如前所述,命令 line properties 始终优先于其他 property 源。

    如果您不希望将 line properties 命令添加到Environment,则可以使用SpringApplication.setAddCommandLineProperties(false)禁用它们。

    Application Property Files

    SpringApplication在以下位置从application.properties files 加载 properties 并将它们添加到 Spring Environment

    • 当前目录的/config子目录 (在src上面一层)

    • 当前目录 (在src上面一层)

    • 一个 classpath /config

    • classpath 根目录

    该列表按优先级排序(在列表中较高位置定义的 properties 将覆盖在较低位置中定义的位置)。

    你也可以使用 YAML('.yml')files作为'.properties'的替代品。

    如果您不喜欢application.properties作为 configuration 文件 name,则可以通过指定spring.config.name environment property 切换到另一个文件 name。您还可以使用spring.config.location environment property(这是目录位置的 comma-separated 列表或文件_path)来引用显式位置。以下 example 显示了如何指定不同的文件 name:

    $ java -jar myproject.jar --spring.config.name=myproject

    以下 example 显示了如何指定两个位置:

    $ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

    很早就会使用spring.config.namespring.config.location来确定必须加载哪些 files,因此必须将它们定义为环境 property(通常是 OS 环境变量,系统 property 或 command-line 参数)。

    如果spring.config.location包含目录(而不是 files),它们应该以/结尾(并且在运行时,在加载之前附加从spring.config.name生成的名称,包括 profile-specific 文件名)。 spring.config.location中指定的 Files 使用 as-is,不支持 profile-specific 变体,并被任何 profile-specific properties 覆盖。

    在反向 order 中搜索配置位置。默认情况下,配置的位置为classpath:/,classpath:/config/,file:./,file:./config/。生成的 search order 如下:

    • file:./config/

    • file:./

    • classpath:/config/

    • classpath:/

    使用spring.config.location配置自定义配置位置时,它们会替换默认位置。对于 example,如果使用 value classpath:/custom-config/,file:./custom-config/配置了spring.config.location,则 search order 将变为以下内容:

    • file:./custom-config/

    • classpath:custom-config/

    或者,当使用spring.config.additional-location配置自定义配置位置时,除默认位置外,还会使用它们。在默认位置之前搜索其他位置。例如,如果配置了classpath:/custom-config/,file:./custom-config/的其他位置,则 search order 将变为以下内容:

    • file:./custom-config/

    • classpath:custom-config/

    • file:./config/

    • file:./

    • classpath:/config/

    • classpath:/

    此搜索 ordering 允许您在一个 configuration 文件中指定默认值,然后有选择地在另一个 configuration 文件中覆盖这些值。您可以在其中一个默认位置为application.properties(或您使用spring.config.name选择的任何其他基本名称)中的 application 提供默认值。然后,可以在运行时使用位于其中一个自定义位置的不同文件覆盖这些默认值。

    如果使用环境变量而不是 system properties,则大多数操作系统都不允许使用 period-separated key 名称,但您可以使用下划线(对于 example,SPRING_CONFIG_NAME而不是spring.config.name)。

    如果 application 在容器中运行,则可以使用 JNDI properties(在java:comp/env中)或 servlet context 初始化参数来代替环境变量或系统 properties。

    Profile-specific Properties

    除了application.properties files 之外,还可以使用以下命名约定来定义 profile-specific properties:application-{profile}.properties。如果没有设置 active profiles,则Environment有一组默认的 profiles(默认为[default])。换句话说,如果没有显式激活 profiles,则会加载application-default.properties中的 properties。

    Profile-specific properties 从与标准application.properties相同的位置加载,profile-specific files 始终覆盖 non-specific files,无论 profile-specific files 是否在打包的 jar 内部或外部。

    如果指定了多个 profiles,则应用 last-wins 策略。对于 example,spring.profiles.active property 指定的 profiles 将添加到通过SpringApplication API 配置的 profile 之后,因此优先。

    如果您在spring.config.location中指定了任何 files,则不会考虑这些 files 的 profile-specific 变体。如果您还想使用 profile-specific properties,请使用spring.config.location中的目录。

    Properties 中的占位符

    application.properties中的值在使用时通过现有的Environment进行过滤,因此您可以返回先前定义的值(对于 example,来自 System properties)。

    app.name=MyApp
    app.description=${app.name} is a Spring Boot application
     
  • 相关阅读:
    rpc的几种服务端模型分析
    ASP.NET MVC3在Visual Studio 2010中的变化
    HTML元素的ID和Name属性的区别
    Oracle日期类操作(格式 加减乘 取毫秒)
    快速原型工具
    C#4.0新特性dynamic\可选参数、命名参数
    WCF注册Windows Service
    不要在using语句中调用WCF服务
    C# 4.0 新特性dynamic、可选参数、命名参数等
    Windows内置系统账户Local system/Network service/Local Service
  • 原文地址:https://www.cnblogs.com/dalianpai/p/11772649.html
Copyright © 2020-2023  润新知