[英]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. 我的Spring Boot应用程序具有以下属性文件。
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. 是否可以加载“ common / env.properties”,以便使用给定的特定于环境的属性文件解析其占位符。 For DEV environment, we want the placeholders in "common/env.properties" to be resolved using values from "DEV/env.properties".
对于DEV环境,我们希望使用“ DEV / env.properties”中的值来解析“ common / 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 : 2个选项:
common/application.properties
using configuration-maven-plugin
and filter files for each environment. configuration-maven-plugin
生成common/application.properties
并为每个环境过滤文件。 It is outdated now. application-<env>.properties
for each environment and pass the -Dspring.profiles.active=<env>
as VM option in application start up. application-<env>.properties
,并在应用程序启动时将-Dspring.profiles.active=<env>
作为VM选项传递。 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. 在选项2中,您将使用application-.properties覆盖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 您的
application.properties
可以具有
logging.level.root=WARN
logging.level.org.apache=WARN
logging.level.org.springframework=WARN
Your application-dev.properties
can have 您的
application-dev.properties
可以具有
logging.level.org.springframework=DEBUG
which means, when you are starting application using dev
profile, spring takes 这意味着,当您使用
dev
配置文件启动应用程序时,spring需要
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).
(Spring将使用config-dev.properties中的值覆盖config.properties中的值)。
ignoreResourceNotFound
will make sure, application will still start with default values even if the corresponding file is not found. ignoreResourceNotFound
将确保即使未找到对应的文件,应用程序仍将以默认值启动。
@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
然后使用命令行变量
-env=dev
启动spring boot应用程序
UPDATE UPDATE
You can use @PropertySources annotation to load several properties. 您可以使用@PropertySources批注来加载多个属性。
@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. 您可以添加resources / application.yml文件,其中一个文件中可以包含多个配置文件。 MultiProfile Yaml eghere are two different profiles 'dev' and 'qa' with different applicationNames 'DEV' and 'QA' and one defaultName 'Default'
MultiProfile Yaml例如,这里是两个不同的配置文件'dev'和'qa',具有不同的applicationNames'DEV'和'QA'和一个defaultName'Default'
spring:
application:
name: Default
profiles:
active: qa
---
spring:
profiles: dev
application:
name: DEV
---
spring:
profiles: qa
application:
name: QA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.