Spring Boot 和多个外部配置文件 [英] Spring Boot and multiple external configuration files

查看:83
本文介绍了Spring Boot 和多个外部配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个要从类路径加载的属性文件./src/main/resources 下有一个默认设置,它是 myapp.jar 的一部分.我的 springcontext 期望文件在类路径上.即

I have multiple property files that I want to load from classpath. There is one default set under /src/main/resources which is part of myapp.jar. My springcontext expects files to be on the classpath. i.e.

<util:properties id="Job1Props"
    location="classpath:job1.properties"></util:properties>

<util:properties id="Job2Props"
    location="classpath:job2.properties"></util:properties>

我还需要使用外部集覆盖这些属性的选项.我在 cwd 中有一个外部配置文件夹.根据 spring boot doc config 文件夹应该在类路径上.但从文档中不清楚它是否只会覆盖 applicaiton.properties 从那里或配置中的所有属性.

I also need the option to override these properties with an external set. I have an external config folder in cwd. As per spring boot doc config folder should be on classpath. But its not clear from doc if it will only override the applicaiton.properties from there or all the properties in config.

当我测试它时,只有 application.properties 被选取,其余的属性仍然从 /src/main/resources 选取.我尝试将它们作为逗号分隔列表提供给 spring.config.location,但默认设置仍未被覆盖.

When I tested it, only application.properties gets picked up and rest of properties are still picked up from /src/main/resources. I have tried supplying them as comma separated list to spring.config.location but the default set is still not being overriden.

如何让多个外部配置文件覆盖默认配置文件?

How do I make mulitiple external config files override default ones?

作为解决方法,我目前使用的是通过命令行提供的 app.config.location(应用程序特定属性).即

As workaround I currently used app.config.location (app specific property) which I supply through the command line. i.e

java -jar myapp.jar app.config.location=file:./config

并且我将我的 applicationcontext 更改为

and I changed my applicationcontext to

<util:properties id="Job2Props"
    location="{app.config.location}/job2.properties"></util:properties>

这就是我在加载应用程序时分离文件和类路径的方式.

And this is how I make separation between file and classpath while loading Application.
EDITS:

//psuedo code

if (StringUtils.isBlank(app.config.location)) {
            System.setProperty(APP_CONFIG_LOCATION, "classpath:");
}

我真的不想使用上述解决方法,而是让 spring 覆盖类路径上的所有外部配置文件,就像它对 application.properties 文件所做的那样.

I would really like not to use the above workaround and have spring override all external config files on the classpath like it does for the application.properties file.

推荐答案

更新:由于 spring.config.location 的行为现在覆盖默认值而不是添加到默认值.您需要使用 spring.config.additional-location 来保持默认值.这是从 1.x 到 2.x 的行为变化

UPDATE: As the behaviour of spring.config.location now overrides the default instead of adding to it. You need to use spring.config.additional-location to keep the defaults. This is a change in behaviour from 1.x to 2.x

当使用 Spring Boot 时,属性按以下顺序加载(参见 Spring Boot 参考指南中的外部化配置).

When using Spring Boot the properties are loaded in the following order (see Externalized Configuration in the Spring Boot reference guide).

  1. 命令行参数.
  2. Java 系统属性 (System.getProperties()).
  3. 操作系统环境变量.
  4. 来自 java:comp/env 的 JNDI 属性
  5. 仅具有随机属性的 RandomValuePropertySource.*.
  6. 打包 jar 之外的应用程序属性(application.properties 包括 YAML 和配置文件变体).
  7. 打包在 jar 中的应用程序属性(application.properties 包括 YAML 和配置文件变体).
  8. @Configuration 类上的@PropertySource 注释.
  9. 默认属性(使用 SpringApplication.setDefaultProperties 指定).

解析属性时(即 @Value("${myprop}") 解析以相反的顺序完成(所以从 9 开始).

When resolving properties (i.e. @Value("${myprop}") resolving is done in the reverse order (so starting with 9).

要添加不同的文件,您可以使用 spring.config.location 属性,该属性采用逗号分隔的属性文件列表或文件位置(目录).

To add different files you can use the spring.config.location properties which takes a comma separated list of property files or file location (directories).

-Dspring.config.location=your/config/dir/

上面的将添加一个目录,该目录将用于查询 application.properties 文件.

The one above will add a directory which will be consulted for application.properties files.

-Dspring.config.location=classpath:job1.properties,classpath:job2.properties

这会将 2 个属性文件添加到加载的文件中.

This will add the 2 properties file to the files that are loaded.

默认配置文件和位置在附加指定的 spring.config.location 之前加载,这意味着后者将始终覆盖在早期配置中设置的属性.(另见 Spring Boot 参考指南的这一部分).

The default configuration files and locations are loaded before the additonally specified spring.config.location ones meaning that the latter will always override properties set in the earlier ones. (See also this section of the Spring Boot Reference Guide).

如果 spring.config.location 包含目录(而不是文件),它们应该以/结尾(并且会附加从 spring.config.name 在加载之前).始终使用默认搜索路径 classpath:,classpath:/config,file:,file:config/,与 spring.config.location 的值无关.通过这种方式,您可以在 application.properties(或您使用 spring.config.name 选择的任何其他基本名称)中为您的应用程序设置默认值,并在运行时使用一个不同的文件,保留默认值.

If spring.config.location contains directories (as opposed to files) they should end in / (and will be appended with the names generated from spring.config.name before being loaded). The default search path classpath:,classpath:/config,file:,file:config/ is always used, irrespective of the value of spring.config.location. In that way you can set up default values for your application in application.properties (or whatever other basename you choose with spring.config.name) and override it at runtime with a different file, keeping the defaults.

这篇关于Spring Boot 和多个外部配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆