How to resolve placeholder in properties file with values from another properties file in spring boot application

浪子不回头ぞ 提交于 2019-12-02 08:33:47

问题


My spring boot application has below properties files.

src/main/resources/config/DEV/env.properties
mail.server=dev.mail.domain

src/main/resources/config/QA/env.properties
mail.server=qa.mail.domain

src/main/resources/config/common/env.properties
mail.url=${mail.server}/endpoint

Is it possible to load "common/env.properties" so that it's placeholders will be resolved using the given environment specific properties file. For DEV environment, we want the placeholders in "common/env.properties" to be resolved using values from "DEV/env.properties".

There are answers about how to load multiple properties files and profile based loading but could not find an answer for this particular use case.

Thanks in advance.


回答1:


2 Options :

  1. Generate the common/application.properties using configuration-maven-plugin and filter files for each environment. It is outdated now.
  2. Use application-<env>.properties for each environment and pass the -Dspring.profiles.active=<env> as VM option in application start up. Spring will automatically take the property from correct file.

In option 2, you will be overwriting whatever is present in application.properties with application-.properties. So you dont have to add only the properties which you need to change per environment.

for eg:

Your application.properties can have

logging.level.root=WARN
logging.level.org.apache=WARN
logging.level.org.springframework=WARN

Your application-dev.properties can have

logging.level.org.springframework=DEBUG

which means, when you are starting application using dev profile, spring takes

logging.level.root=WARN
logging.level.org.apache=WARN
logging.level.org.springframework=DEBUG

edit :

Also, you can try something like below on your class. (Spring will overwrite value in config.properties with values from config-dev.properties). ignoreResourceNotFound will make sure, application will still start with default values even if the corresponding file is not found.

@Configuration
@PropertySource("classpath:config.properties")
@PropertySource(value = "classpath:config-${spring.profiles.active}.properties", ignoreResourceNotFound = true)



回答2:


You can add resources/application.yml file where you can have multiple profiles in one File. MultiProfile Yaml e.g.here are two different profiles 'dev' and 'qa' with different applicationNames 'DEV' and 'QA' and one defaultName 'Default'

spring:
  application:
    name: Default
  profiles:
    active: qa

---
spring:
  profiles: dev
  application:
    name: DEV
---
spring:
  profiles: qa
  application:
    name: QA



回答3:


You can achieve this by declaring a property source on a class configuration and setting up an environment variable in the path :

@PropertySource({ "classpath:config/${env}/env.properties" })
@Configuration
public class config{}

And then you launch the spring boot app with the command line variable -env=dev

UPDATE

You can use @PropertySources annotation to load several properties.

 @PropertySources({
    @PropertySource("classpath:config/${env}/env.properties"),
    @PropertySource("classpath:config/common/env.properties")
  })
  public class config{}


来源:https://stackoverflow.com/questions/54883144/how-to-resolve-placeholder-in-properties-file-with-values-from-another-propertie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!