如何在Spring-Boot生产期间覆盖application.properties?

mem*_*und 41 java spring spring-boot

我正在使用spring boot并application.properties在开发过程中选择一个数据库@Configuration @Profile("dev").

spring.profiles.active=dev
spring.config.location=file:d:/application.properties
Run Code Online (Sandbox Code Playgroud)

在生产过程中,我想创建一个应该加载的应用程序上下文之外的文件,然后使用d:/application.properties激活不同的配置文件:

spring.profiles.active=production
Run Code Online (Sandbox Code Playgroud)

结果:当我启动应用程序时,配置仍然是dev,因此不会考虑生产属性文件的其他位置.我错过了什么吗?

弹簧靴1.1.0.BUILD-SNAPSHOT

注意:这个问题不是关于tomcat的.

Kal*_*oni 18

我知道你问过怎么做.

但答案是,你不应该这样做.

您可以拥有application.properties application-default.properties application-dev.properties等

您可以通过命令行参数将配置文件切换到jvm

你可以使用@TestPropertySource在测试时覆盖一些东西

理想情况下,所有内容都应该在源代码管理中,这样就不会有任何意外 - 您如何知道服务器位置中存在哪些属性,以及缺少哪些属性.如果开发人员介绍新东西会发生什么

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Spring boot已经为你提供了足够的方法来做到这一点.

  • 虽然使用 spring 配置文件对配置值集进行分组可能是 Spring/Java 中的常见做法,但需要指出的是,这并不是现代服务和应用程序的最佳实践,尤其是那些旨在遵循云原生实践(例如 https 中描述的)的服务和应用程序。 ://12factor.net/config。 (3认同)
  • 虽然这个问题来自我使用spring-boot的早期时代,但我现在同意在运行jar / war时,只能通过命令行参数“ spring.profiles.active = production”来激活配置文件。因此,我将接受您的回答。 (2认同)
  • 如果个人资料设置为“生产”,“ application.properties”是否会运行? (2认同)
  • 是的,它将执行应用程序和应用程序生产,如果属性相同,则以后将作为覆盖工作 (2认同)

Sau*_*abh 11

你也可以使用 @PropertySources

@PropertySources({
        @PropertySource(value = "classpath:application.properties"),
        @PropertySource(value = "file:/user/home/external.properties", ignoreResourceNotFound = true)
})
public class Application {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);

    }


}
Run Code Online (Sandbox Code Playgroud)

  • 对我有用,有两点注释:1.第二个资源中的定义覆盖第一个资源中的定义。2.除非第一个资源被命名为“application.properties”(至少在Spring版本4.3.20中)似乎Spring 4有一个阻止覆盖的错误。只需重命名 `application.properties` -> `app.properties` 就可以了。 (2认同)

Gab*_*uiu 10

我不确定您是否可以动态更改配置文件.

为什么不将spring.config.location属性设置为所需的外部位置的内部属性文件,并且该位置(jar外部)的属性文件是否设置了spring.profiles.active属性?

更好的是,有一个特定于dev配置文件的内部属性文件(具有spring.profiles.active = dev)并保持原样,当你想在生产中部署时,为你的属性文件指定一个新的位置,它有spring .profiles.active = PROD:

java -jar myjar.jar --spring.config.location=D:\wherever\application.properties
Run Code Online (Sandbox Code Playgroud)

  • 是的,这是一个选项,但我想防止在启动时必须给出任何cmd args.特别是春季靴子声称支持这个...... (3认同)

Sel*_*amy 8

从 Spring Boot 2 开始,您将不得不使用

--spring.config.additional-location=production.properties
Run Code Online (Sandbox Code Playgroud)


Sur*_*oen 6

使用 Spring Boot 2.2.2.Release 进行更新。

完整示例,https://www.surasint.com/spring-boot-override-property-example/

假设,在您的 jar 文件中,您的 application.properties 包含以下两行:

server.servlet.context-path=/test
server.port=8081
Run Code Online (Sandbox Code Playgroud)

然后,在生产中,您希望覆盖 server.port=8888 但您不想覆盖其他属性。

首先,您创建另一个文件,例如 override.properties 并在线设置这一行:

server.port=8888
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样启动jar

java -jar spring-boot-1.0-SNAPSHOT.jar --spring.config.location=classpath:application.properties,/opt/somewhere/override.properties
Run Code Online (Sandbox Code Playgroud)


das*_*nga 5

更新:这是春天的错误,请参见此处

jar外部的应用程序属性必须位于以下位置之一,然后一切正常。

21.2 Application property files
SpringApplication will load properties from application.properties files in the following    locations and add them to the Spring Environment:

A /config subdir of the current directory.
The current directory
A classpath /config package
The classpath root
Run Code Online (Sandbox Code Playgroud)

因此,例如,当您不想指定cmd行参数并且不在基本app.props中使用spring.config.location时,这应该可以工作:

d:\yourExecutable.jar
d:\application.properties

or

d:\yourExecutable.jar
d:\config\application.properties
Run Code Online (Sandbox Code Playgroud)

请参阅spring外部配置文档

更新:您可以将\ @Configuration与\ @PropertySource一起使用。根据此处的文档您可以在任何地方指定资源。您应该小心一点,在加载哪个配置时要确保您的产品获胜。


avi*_*rat 5

我发现以下内容对我有用:

java -jar my-awesome-java-prog.jar --spring.config.location=file:/path-to-config-dir/
Run Code Online (Sandbox Code Playgroud)

file:添加。

后期编辑

当然,这个命令行永远不会像在生产中那样运行。

而是我有

  • [可能有几层]shell源代码控制中的脚本,带有占位符的命令的所有部分都可以更改(jar 的名称、配置的路径...)
  • ansible将部署shell脚本并用实际值替换占位符的部署脚本。