前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot配置文件加载顺序

springboot配置文件加载顺序

作者头像
六月的雨在Tencent
发布2024-03-28 20:09:35
1190
发布2024-03-28 20:09:35
举报
文章被收录于专栏:CSDNCSDN
springboot配置文件加载顺序

  • springboot配置文件加载顺序

springboot配置文件加载顺序

官方文档如下说明:


SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:

1. A /config subdirectory of the current directory 2. The current directory 3. A classpath /config package 4. The classpath root The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

You can also use YAML (’.yml’) files as an alternative to ‘.properties’.

If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths). The following example shows how to specify a different file name:

代码语言:javascript
复制
$ java -jar myproject.jar --spring.config.name=myproject

The following example shows how to specify two locations:

代码语言:javascript
复制
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

spring.config.name and spring.config.location are used very early to determine which files have to be loaded. They must be defined as an environment property (typically an OS environment variable, a system property, or a command-line argument).

If spring.config.location contains directories (as opposed to files), they should end in / (and, at runtime, be appended with the names generated from spring.config.name before being loaded, including profile-specific file names). Files specified in spring.config.location are used as-is, with no support for profile-specific variants, and are overridden by any profile-specific properties.

Config locations are searched in reverse order. By default, the configured locations are classpath:/,classpath:/config/,file:./,file:./config/. The resulting search order is the following:

1 file:./config/ 2 file:./ 3 classpath:/config/ 4 classpath:/ When custom config locations are configured by using spring.config.location, they replace the default locations. For example, if spring.config.location is configured with the value classpath:/custom-config/,file:./custom-config/, the search order becomes the following:

1 file:./custom-config/ 2 classpath:custom-config/ Alternatively, when custom config locations are configured by using spring.config.additional-location, they are used in addition to the default locations. Additional locations are searched before the default locations. For example, if additional locations of classpath:/custom-config/,file:./custom-config/ are configured, the search order becomes the following:

1 file:./custom-config/ 2 classpath:custom-config/ 3 file:./config/ 4 file:./ 5 classpath:/config/ 6 bclasspath:/ This search ordering lets you specify default values in one configuration file and then selectively override those values in another. You can provide default values for your application in application.properties (or whatever other basename you choose with spring.config.name) in one of the default locations. These default values can then be overridden at runtime with a different file located in one of the custom locations.

If you use environment variables rather than system properties, most operating systems disallow period-separated key names, but you can use underscores instead (for example, SPRING_CONFIG_NAME instead of spring.config.name).

If your application runs in a container, then JNDI properties (in java:comp/env) or servlet context initialization parameters can be used instead of, or as well as, environment variables or system properties.


工具翻译后为: SpringApplication从application.properties以下位置的文件加载属性并将其添加到Spring中Environment:

1. 一个/config当前目录的子目录 2. 当前目录 3. 类路径/config包 4. 类路径根 该列表按优先级排序(在列表较高位置定义的属性会覆盖在较低位置定义的属性)。

您还可以使用YAML(.yml)文件来替代.properties。

如果您不喜欢application.properties配置文件名,则可以通过指定spring.config.name环境属性来切换到另一个文件名。您还可以使用spring.config.location环境属性(目录位置或文件路径的逗号分隔列表)来引用显式位置。下面的示例演示如何指定其他文件名:

代码语言:javascript
复制
$ java -jar myproject.jar --spring.config.name = myproject

下面的示例演示如何指定两个位置:

代码语言:javascript
复制
$ java -jar myproject.jar --spring.config.location = classpath:/default.properties,classpath:/override.properties

spring.config.name并且spring.config.location很早就用于确定必须加载哪些文件。必须将它们定义为环境属性(通常是OS环境变量,系统属性或命令行参数)。

如果spring.config.location包含目录(而不是文件),则应以目录结尾/(并且在运行时,在目录后附加从生成spring.config.name之前生成的名称,包括特定于配置文件的文件名)。指定的文件spring.config.location按原样使用,不支持特定于配置文件的变体,并且被任何特定于配置文件的属性覆盖。

配置位置以相反的顺序搜索。默认情况下,配置的位置是classpath:/,classpath:/config/,file:./,file:./config/。结果搜索顺序如下:

1 file:./config/ 2 file:./ 3 classpath:/config/ 4 classpath:/ 当使用来配置自定义配置位置时spring.config.location,它们将替换默认位置。例如,如果spring.config.location使用值配置classpath:/custom-config/,file:./custom-config/,则搜索顺序将变为以下内容:

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

1 file:./custom-config/ 2 classpath:custom-config/ 3 file:./config/ 4 file:./ 5 classpath:/config/ 6 classpath:/ 通过此搜索顺序,您可以在一个配置文件中指定默认值,然后在另一个配置文件中有选择地覆盖这些值。您可以在以下默认位置之一中为您的应用程序提供默认值application.properties(或使用来选择的其他任何基本名称spring.config.name)。然后,可以在运行时使用自定义位置之一中的其他文件覆盖这些默认值。

如果您使用环境变量而不是系统属性,则大多数操作系统都不允许使用句点分隔的键名,但是您可以使用下划线代替(例如,SPRING_CONFIG_NAME代替spring.config.name)。

如果您的应用程序在容器中运行,则可以使用JNDI属性(中的java:comp/env)或Servlet上下文初始化参数来代替环境变量或系统属性,也可以使用它们。

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • springboot配置文件加载顺序
  • springboot配置文件加载顺序
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com