如何使用Spring Boot应用程序中另一个属性文件中的值解析属性文件中的占位符 [英] How to resolve placeholder in properties file with values from another properties file in spring boot application

查看:123
本文介绍了如何使用Spring Boot应用程序中另一个属性文件中的值解析属性文件中的占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的spring boot应用程序具有以下属性文件。

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

是否可以加载 common / env.properties,以便使用给定的环境特定属性文件解析其占位符。对于DEV环境,我们希望使用 DEV / env.properties中的值来解析 common / env.properties中的占位符。

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.

预先感谢。

推荐答案

2个选项:


  1. 生成 common / application.properties 使用 configuration-maven-plugin 并为每个环境过滤文件。

  2. 在每个环境中使用 application-< env> .properties 并传递 -Dspring.profiles.active =< env> 作为应用程序启动中的VM选项。 Spring将自动从正确的文件中获取属性。

  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.

在选项2中,您将使用application-.properties覆盖application.properties中存在的内容。因此,您不必仅添加每个环境需要更改的属性。

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.

例如:

您的 application.properties 可以具有

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

您的 application-dev.properties 可以具有

logging.level.org.springframework=DEBUG

这意味着,当您使用<$ c启动应用程序时$ c> dev 配置文件,春季需要

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:

此外,您可以在课堂上尝试以下类似的方法。 (Spring将使用config-dev.properties中的值覆盖config.properties中的值)。 ignoreResourceNotFound 将确保即使没有找到相应的文件,应用程序仍将以默认值启动。

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)

这篇关于如何使用Spring Boot应用程序中另一个属性文件中的值解析属性文件中的占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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