问题描述
我正在开发Spring Boot应用程序
我想用外部文件(例如/etc/projectName/application.properties
)覆盖 src/main/resources/application.properties
中的某些属性./p>
我尝试了几种方法:
-
@PropertySource("file:/etc/projectName/application.properties")
作为ApplicationConfig.java
上的注释 -
spring.config.location =/etc/projectName/application.properties
在我的application.properties中的资源
我用 spring.port
进行了测试.第一种方法仅添加属性,但不覆盖它们.
我总是按照文档,您可以在其中放置各种文件,一个具有默认值,另一个具有被覆盖的值.
另外,您也可以使用类似:
@PropertySources({@PropertySource("classpath:default.properties"),@PropertySource(值="file:$ {external.config}",ignoreResourceNotFound = true)})
,然后在application.properties中指定 external.config
.
这将提供覆盖配置的默认路径,通过在命令行中指定-external.config
,它本身仍可以覆盖.
我将 $ {external.config}
定义为系统环境变量,但它也应与application.properties变量一起使用.
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
).
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
I tested it with spring.port
. The first method only added properties but didn't override them.
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.
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.
这篇关于Spring Boot外部application.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!