简体   繁体   中英

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

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.

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)

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{}

You can add resources/application.yml file where you can have multiple profiles in one File. MultiProfile Yaml eghere 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM