Java 如何在 Spring-Boot 的生产过程中覆盖 application.properties?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23563363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to override application.properties during production in Spring-Boot?
提问by membersound
I'm using spring boot and application.properties
to select a database during development by @Configuration @Profile("dev")
.
我正在使用 spring boot 并application.properties
在开发过程中选择一个数据库@Configuration @Profile("dev")
。
spring.profiles.active=dev
spring.config.location=file:d:/application.properties
During production I'd like to create a file outside of the application context that should be loaded and then active a different configuration profile, with d:/application.properties:
在生产期间,我想在应用程序上下文之外创建一个文件,该文件应该被加载,然后使用 d:/application.properties 激活不同的配置文件:
spring.profiles.active=production
Result: when I start the app, the configuration is still dev
, so somehow the additional location of the productive properties file is not taken into account. Am I missing anything?
结果:当我启动应用程序时,配置仍然是dev
,所以不知何故不考虑生产属性文件的附加位置。我错过了什么吗?
spring boot 1.1.0.BUILD-SNAPSHOT
弹簧靴 1.1.0.BUILD-SNAPSHOT
Note: this question is NOTabout tomcat.
注意:这个问题是不是关于tomcat的。
采纳答案by Kalpesh Soni
I know you asked how to do this, but the answer is you should not do this.
我知道你问过如何做到这一点,但答案是你不应该这样做。
Instead, have a application.properties
, application-default.properties
application-dev.properties
etc., and switch profiles via args to the JVM: e.g. -Dspring.profiles.active=dev
取而代之的是,有一个application.properties
,application-default.properties
application-dev.properties
等,并且经由ARGS切换配置文件到JVM:例如-Dspring.profiles.active=dev
You can also override some things at test time using @TestPropertySource
您还可以在测试时使用 @TestPropertySource
Ideally everything should be in source control so that there are no surprises e.g. How do you know what properties are sitting there in your server location, and which ones are missing? What happens if developers introduce new things?
理想情况下,一切都应该在源代码控制中,这样就不会出现意外,例如,您如何知道您的服务器位置中有哪些属性,哪些丢失了?如果开发人员引入新事物会怎样?
Spring Boot is already giving you enough ways to do this right.
Spring Boot 已经为您提供了足够的方法来做到这一点。
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
回答by Gabriel Ruiu
I am not sure you can dynamically change profiles.
我不确定您是否可以动态更改配置文件。
Why not just have an internalproperties file with the spring.config.locationproperty set to your desired outside location, and the properties file at that location (outside the jar) have the spring.profiles.activeproperty set?
为什么不只是将spring.config.location属性设置为所需的外部位置的内部属性文件,并且该位置(jar 外部)的属性文件设置spring.profiles.active属性?
Better yet, have an internal properties file, specific to dev profile (has spring.profiles.active=dev) and leave it like that, and when you want to deploy in production, specify a new location for your properties file, which has spring.profiles.active=prod:
更好的是,有一个内部属性文件,特定于开发配置文件(具有 spring.profiles.active=dev)并保持原样,当您想在生产中部署时,为您的属性文件指定一个新位置,其中包含 spring .profiles.active=产品:
java -jar myjar.jar --spring.config.location=D:\wherever\application.properties
回答by dasAnderl ausMinga
UPDATE: this is a bug in spring see here
更新:这是春天的一个错误,见这里
the application properties outside of your jar must be in one of the following places, then everything should work.
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
so e.g. this should work, when you dont want to specify cmd line args and you dont use spring.config.location in your base app.props:
因此,例如,当您不想指定 cmd 行参数并且不在基本 app.props 中使用 spring.config.location 时,这应该可以工作:
d:\yourExecutable.jar
d:\application.properties
or
d:\yourExecutable.jar
d:\config\application.properties
see spring external config doc
Update: you may use \@Configuration together with \@PropertySource. according to the doc hereyou can specify resources anywhere. you should just be careful, when which config is loaded to make sure your production one wins.
更新:您可以将 \@Configuration 与 \@PropertySource 一起使用。根据此处的文档,您可以在任何地方指定资源。您应该小心,何时加载哪个配置以确保您的产品获胜。
回答by Saurabh
You can also use @PropertySources
你也可以使用 @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);
}
}
回答by avi.elkharrat
I have found the following has worked for me:
我发现以下内容对我有用:
java -jar my-awesome-java-prog.jar --spring.config.location=file:/path-to-config-dir/
with file:
added.
与file:
添加。
LATE EDIT
后期编辑
Of course, this command line is neverrun as it is in production.
当然,这个命令行永远不会像在生产中那样运行。
Rather I have
而是我有
- [possibly several layers of]
shell
scripts in source control with place holders for all parts of the command that could change (name of the jar, path to config...) ansible
deployment scripts that will deploy theshell
scripts and replace the place holders by the actual value.
- [可能有几层]
shell
源代码控制中的脚本,带有占位符的命令的所有部分都可以更改(jar 的名称、配置的路径...) ansible
将部署shell
脚本并用实际值替换占位符的部署脚本。
回答by Himadri Mandal
The spring configuration precedence is as follows.
spring 配置优先级如下。
- ServletConfig init Parameter
- ServletContext init parameter
- JNDI attributes
- System.getProperties()
- ServletConfig 初始化参数
- ServletContext 初始化参数
- JNDI 属性
- System.getProperties()
So your configuration will be overridden at the command-line if you wish to do that. But the recommendation is to avoid overriding, though you can use multiple profiles.
因此,如果您希望这样做,您的配置将在命令行中被覆盖。但建议避免覆盖,尽管您可以使用多个配置文件。
回答by Selvakumar Ponnusamy
From Spring Boot 2, you will have to use
从 Spring Boot 2 开始,您将不得不使用
--spring.config.additional-location=production.properties
回答by Surasin Tancharoen
Update with Spring Boot 2.2.2.Release.
使用 Spring Boot 2.2.2.Release 进行更新。
Full example here, https://www.surasint.com/spring-boot-override-property-example/
完整示例,https://www.surasint.com/spring-boot-override-property-example/
Assume that, in your jar file, you have the application.properties which have these two line:
假设在您的 jar 文件中,您的 application.properties 包含以下两行:
server.servlet.context-path=/test
server.port=8081
Then, in production, you want to override the server.port=8888 but you don't want to override the other properties.
然后,在生产中,您希望覆盖 server.port=8888 但您不想覆盖其他属性。
First you create another file, ex override.properties and have online this line:
首先,您创建另一个文件,例如 override.properties 并在线设置这一行:
server.port=8888
Then you can start the jar like this
然后你可以像这样启动jar
java -jar spring-boot-1.0-SNAPSHOT.jar --spring.config.location=classpath:application.properties,/opt/somewhere/override.properties