Spring Boot外部application.properties [英] Spring boot external application.properties

查看:87
本文介绍了Spring Boot外部application.properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Spring Boot应用程序

我想用外部文件(例如/etc/projectName/application.properties )覆盖 src/main/resources/application.properties 中的某些属性./p>

我尝试了几种方法:

  1. @PropertySource("file:/etc/projectName/application.properties")作为 ApplicationConfig.java

  2. 上的注释
  3. spring.config.location =/etc/projectName/application.properties 在我的application.properties中的资源

我用 spring.port 进行了测试.第一种方法仅添加属性,但不覆盖它们.

解决方案

我总是按照

,然后在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:

  1. @PropertySource("file:/etc/projectName/application.properties") as annotation at ApplicationConfig.java

  2. spring.config.location=/etc/projectName/application.properties in my application.properties in resources

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆