问题
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 :
- Generate the
common/application.properties
usingconfiguration-maven-plugin
and filter files for each environment. It is outdated now. - 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