Java Spring Boot 外部 application.properties
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27770852/
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
Spring boot external application.properties
提问by greenhalos
I am developing a spring boot application
我正在开发一个弹簧启动应用程序
I want to override some properties in src/main/resources/application.properties
with an external file (e.g. /etc/projectName/application.properties
).
我想src/main/resources/application.properties
用外部文件(例如/etc/projectName/application.properties
)覆盖一些属性。
I tried several methods:
我尝试了几种方法:
@PropertySource("file:/etc/projectName/application.properties")
as annotation atApplicationConfig.java
spring.config.location=/etc/projectName/application.properties
in my application.properties inresources
@PropertySource("file:/etc/projectName/application.properties")
作为注释在ApplicationConfig.java
spring.config.location=/etc/projectName/application.properties
在我的 application.properties 中resources
I tested it with spring.port
. The first method only added properties but didn't override them.
我用spring.port
. 第一种方法只添加属性但没有覆盖它们。
回答by JR Utily
I always use --spring.config.location=
in the command line as specified in the documentation, and you can put various files in this, one with default values and another with the overridden ones.
我总是--spring.config.location=
按照文档中的说明在命令行中使用,您可以在其中放置各种文件,一个带有默认值,另一个带有覆盖的文件。
Edit:
Alternatively, you could also use something like :
编辑:
或者,你也可以使用类似的东西:
@PropertySources({
@PropertySource("classpath:default.properties")
, @PropertySource(value = "file:${external.config}", ignoreResourceNotFound = true)
})
and specify a external.config
in your application.properties.
This would provide a default path for overridding config, that is still overriddable itself by specifying a --external.config
in the command line.
I use this with ${external.config}
being defined as a system env variable, but it should work with a application.properties variable too.
并external.config
在您的 application.properties 中指定一个。
这将为覆盖配置提供默认路径,通过--external.config
在命令行中指定 a 仍然可以覆盖它本身。
我将${external.config}
它定义为系统环境变量,但它也应该与 application.properties 变量一起使用。
回答by Ian Lim
Notes
笔记
1) Tomcat allows you to define context parameters 2) Spring Boot will read the properties defined in Tomcat context parameters as if you are defining them as -Dsomething=some_value
1) Tomcat 允许您定义上下文参数 2) Spring Boot 将读取 Tomcat 上下文参数中定义的属性,就像您将它们定义为 -Dsomething=some_value
Options
选项
Option 1:Hence a possible way is for you to define spring.config.locationat the Tomcat context paramter:
选项 1:因此,一种可能的方法是在 Tomcat 上下文参数中定义spring.config.location:
<Context>
<Parameter name="spring.config.location" value="/path/to/application.properties" />
</Context>
Option 2:define a system property at the Tomcat's setenv.sh file
选项 2:在 Tomcat 的 setenv.sh 文件中定义系统属性
Dspring.config.location=/path/to/application.properties
Option 3:define an environment variable: SPRING_CONFIG_LOCATION
选项 3:定义一个环境变量:SPRING_CONFIG_LOCATION
回答by George Z.
If you do not want to use any solution from above, you could do the following in the start of your main(String args[])
method (before the usage of SpringApplication.run(MyClass.class, args)
:
如果您不想使用上面的任何解决方案,您可以在您的main(String args[])
方法开始时执行以下操作(在使用之前SpringApplication.run(MyClass.class, args)
:
//Specifies a custom location for application.properties file.
System.setProperty("spring.config.location", "target/config/application.properties");
回答by Ramzi Guetiti
I had the same requirement like yours( war package instead of a fat jar) and I manged to externalize the configuration file: In my main class I made this configuration:
我有和你一样的需求(war包而不是一个胖罐子),我设法将配置文件外部化:在我的主类中,我做了这个配置:
@SpringBootApplication
@PropertySource("file:D:\Projets\XXXXX\Config\application.properties")
public class WyApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(WyApplication.class, args);
}
}
Hope this will help you. Good luck.
希望这会帮助你。祝你好运。