简体   繁体   English

如何使用Spring Boot应用程序中另一个属性文件中的值解析属性文件中的占位符

[英]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个选项:

  1. Generate the common/application.properties using configuration-maven-plugin and filter files for each environment. 使用configuration-maven-plugin生成common/application.properties并为每个环境过滤文件。 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. 在每个环境中使用application-<env>.properties ,并在应用程序启动时将-Dspring.profiles.active=<env>作为VM选项传递。 Spring will automatically take the property from correct file. Spring将自动从正确的文件中获取属性。

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.

相关问题 如何在spring中的属性文件中设置占位符值 - How to set placeholder values in properties file in spring Spring Boot从命令行读取属性文件无法解析占位符&#39;ConfigPath&#39; - Spring Boot read properties file from command line Could not resolve placeholder 'ConfigPath' Spring Boot 应用程序:无法解析 application.properties 中的占位符? - Spring Boot app: Could not resolve placeholder in application.properties? 如何在Spring Boot应用程序中使用配置(properties / yml)文件中的属性? - How can I use properties from a configuration (properties/yml) file in my Spring Boot application? Spring Boot从应用程序属性读取值 - Spring Boot read values from application properties Spring boot application.properties 文件读取 - Spring boot application.properties file reading Spring 引导无法识别 application.properties 文件 - Spring Boot not recognizing application.properties file 从 Logback 和 Spring 引导中的应用程序属性文件加载 datePattern - Loading a datePattern from application-properties file in Logback and Spring Boot 春季启动:从“ application.properties”读取文件路径 - Spring boot: Read a file PATH from “application.properties” 为什么 Spring 引导不从默认的 application.properties 文件中读取 - Why is Spring Boot not reading from default application.properties file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM