java - Spring Boot 和多个外部配置文件

标签 java spring config spring-boot

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

<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 文件夹应该在类路径上。但是从文档中不清楚它是否只会从那里覆盖 application.properties 或配置中的所有属性。

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

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

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

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

我将 applicationcontext 更改为

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

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

//pseudo code

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

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

最佳答案

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


使用 Spring Boot 时,属性按以下顺序加载(参见 Spring Boot 引用指南中的 Externalized Configuration)。

  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 开始)。

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

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

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

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

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

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

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.

关于java - Spring Boot 和多个外部配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25855795/

相关文章:

java - 在SDN 5.0.0中创建neo4j sessionfactory

java - 是否可以将自己在运行时动态创建的对象注册为spring bean?

config - tmux : config files are not used

java - 如何修复 - 按 Intent 进行的唯一扩展关联不起作用

java - getLastKnownLocation第一次返回null问题

java - 如何改进这个连接池设计?

Spring Security - 需要 403 错误,而不是重定向

scala - 在 Akka 中配置嵌套路由器

visual-studio-2010 - XDT 配置转换 - ReplaceAll?

java - 使用 RestTemplate 的 Spring Security 身份验证