Springboot常用配置方法

本文深入解析SpringBoot配置机制,涵盖yml与properties文件配置、应用启动参数、VM参数、环境变量及配置优先级等关键概念。揭示配置源的查找顺序与覆盖原则,助您灵活掌控不同环境下的应用配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springboot配置覆盖方法

常用配置方法示例

1. yml(properties)文件配置

项目根目录或者config、或者resource\config文件夹下配置application.yml、application-*.yml(或properties文件)。
在这里插入图片描述

2. 应用启动参数

java -jar AAA args Main方法入口接收参数
在这里插入图片描述

3. Vm启动参数

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

4. 环境变量

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

5.配置优先级

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

在这里插入图片描述另参考
工程(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. 后面覆盖前面同名项目

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值