Springboot配置覆盖方法

常用配置方法示例

1. yml(properties)文件配置

项目根目录或者config、或者resource\config文件夹下配置application.yml、application-*.yml(或properties文件)。

spring boot dao 设置 spring boot如何配置_默认配置

2. 应用启动参数

java -jar AAA args Main方法入口接收参数

spring boot dao 设置 spring boot如何配置_jar_02

3. Vm启动参数

-D配置
java -D***=***
通过System.getProperty(key)获得配置

4. 环境变量

系统环境变量 跟配置JAVA_HOME配置方法一致。
通过System.getenv()获得配置

5.配置优先级

Program args > Vm option > Env > 工程配置文件 > SpringBoot(各Configuration)默认配置

spring boot dao 设置 spring boot如何配置_spring boot dao 设置_03

另参考

工程(yml、properties)文件生效顺序

官方支持的全部配置方法

配置方法

官方原文:https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use a variety of external configuration sources, include Java properties files, YAML files, environment variables, and command-line arguments.

Property values can be injected directly into your beans by using the annotation, accessed through Spring’s abstraction, or be bound to structured objects through .@ValueEnvironment@ConfigurationProperties

Spring Boot uses a very particular order that is designed to allow sensible overriding of values. Properties are considered in the following order (with values from lower items overriding earlier ones):PropertySource

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

属性值可以使用注释直接注入到 Bean 中,通过 Spring 的抽象进行访问,或者通过 绑定到结构化对象。>@ValueEnvironment@ConfigurationProperties

Spring Boot使用非常特殊的顺序,旨在允许对值进行合理的覆盖。按以下顺序考虑属性(较低项的值将覆盖前面的项):PropertySource

  1. Default properties (specified by setting ).SpringApplication.setDefaultProperties

默认属性(由设置 指定)。SpringApplication.setDefaultProperties

  1. @PropertySource annotations on your classes. Please note that such property sources are not added to the until the application context is being refreshed. This is too late to configure certain properties such as and which are read before refresh begins.@ConfigurationEnvironmentlogging.spring.main.

@PropertySource类上的注释。请注意,在刷新应用程序上下文之前,不会将此类属性源添加到 中。配置某些属性为时已晚,无法在刷新开始之前读取这些属性。@ConfigurationEnvironmentlogging.spring.main.

  1. Config data (such as files).application.properties

配置文件数据(如文件)。application.properties

  1. A that has properties only in .RandomValuePropertySourcerandom.*

仅在RandomValuePropertySourcerandom.*中具有的属性。

  1. OS environment variables.

操作系统环境变量。

  1. Java System properties ().System.getProperties()

Java 系统属性

  1. JNDI attributes from .java:comp/env

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

  1. ServletContext init parameters.

ServletContext初始化参数。

  1. ServletConfig init parameters.

ServletConfig初始化参数。

  1. Properties from (inline JSON embedded in an environment variable or system property).SPRING_APPLICATION_JSON

来自 SPRING_APPLICATION_JSON 的属性(嵌入在环境变量或者系统属性【system propert】中的内联 JSON)。

  1. Command line arguments.

命令行参数

  1. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.

properties属性。在@SpringBootTest和用于测试应用程序特定切片的测试注释上可用。

  1. @TestPropertySource annotations on your tests.

在测试中@TestPropertySource注释。

  1. Devtools global settings properties in the directory when devtools is active.$HOME/.config/spring-boot

Devtools 处于活动状态时,目录中的全局设置属性。$HOME/.config/spring-boot

配置文件查找位置

Spring Boot will automatically find and load and files from the following locations when your application starts:application.propertiesapplication.yaml

From the classpath

The classpath root

The classpath package/config

From the current directory

The current directory

The subdirectory in the current directory/config

Immediate child directories of the subdirectory/config

The list is ordered by precedence (with values from lower items overriding earlier ones). Documents from the loaded files are added as to the Spring .PropertySourcesEnvironment


以下为翻译

‎Spring Boot 将在应用程序启动时自动查找并从以下位置加载文件:‎application.propertiesapplication.yaml

1. ‎从类路径‎
    1.1 ‎类路径根目录‎
    1.2 ‎类路径包‎/config
  2. ‎从当前目录‎
    2.1 ‎当前目录‎
    2.2 ‎当前目录中的子目录‎/config
    2.3 ‎子目录的直接子目录‎/config

‎该列表按优先级排序(较低项的值将覆盖较早的项)。加载文件中的文档被添加到 Spring 中。‎PropertySourcesEnvironment

‎如果您不喜欢作为配置文件名,则可以通过指定环境属性切换到另一个文件名。例如,要查找 和 文件,可以按如下方式运行应用程序:‎applicationspring.config.namemyproject.propertiesmyproject.yaml

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

‎还可以使用 environment 属性引用显式位置。此属性接受要检查的一个或多个位置的逗号分隔列表。‎spring.config.location

‎下面的示例演示如何指定两个不同的文件:‎

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

配置文件顺序

Config data files are considered in the following order:

配置文件按以下顺序考虑:

Application properties packaged inside your jar ( and YAML variants).application.properties

打包在 jar 中的应用程序属性(和 YAML 变体)。application.properties

Profile-specific application properties packaged inside your jar ( and YAML variants).application-{profile}.properties

特定于配置文件的应用程序属性打包在 jar(和 YAML 变体)中。application-{profile}.properties

Application properties outside of your packaged jar ( and YAML variants).application.properties

打包的 jar(和 YAML 变体)之外的应用程序属性。application.properties

Profile-specific application properties outside of your packaged jar ( and YAML variants).application-{profile}.properties

打包的 jar 外部特定于配置文件的应用程序属性(和 YAML 变体)。application-{profile}.properties

小总结

需要说明的是,配置文件会同步检查config下的配置,生效顺序config 优先级会更高(配置文件查找顺序决定)。
生效的原则简单总结如下:
1.指定环境优先
2. 外部优先
3. 后面覆盖前面同名项目