spring-boot - 如何实现动态@ConfigurationProperties 前缀

标签 spring-boot

我需要将动态环境名称作为配置属性的前缀传递。我将从命令行将环境作为 VM 参数传递,并且应该为该环境加载所有属性。

我的配置 :

@Configuration
@EnableConfigurationProperties
@PropertySource("environmentDetails.yml")
@ConfigurationProperties(prefix="${environment}")
public class ConfigurationBean {

    private String brokerUrl;
    private String queueName;
    private String receiverUserName;
    private String receiverPassword;

    public String getBrokerUrl() {
        return brokerUrl;
    }
    public void setBrokerUrl(String brokerUrl) {
        this.brokerUrl = brokerUrl;
    }
    public String getQueueName() {
        return queueName;
    }
    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }
    public String getReceiverUserName() {
        return receiverUserName;
    }
    public void setReceiverUserName(String receiverUserName) {
        this.receiverUserName = receiverUserName;
    }
    public String getReceiverPassword() {
        return receiverPassword;
    }
    public void setReceiverPassword(String receiverPassword) {
        this.receiverPassword = receiverPassword;
    }
}

environmentDetails.yml
spring:
  profiles.active: default
---
spring:
  profiles: default

environment:
  brokerUrl: http://ip:port
  queueName: testQueue
  receiverUserName: testuser
  receiverPassword: password

最佳答案

这是问题:您不能将 .yml 与 @PropertySource 一起使用:boot-features-external-config-yaml-shortcomings

YAML files can’t be loaded via the @PropertySource annotation. So in the case that you need to load values that way, you need to use a properties file.



您必须转换为 .properties 才能执行此操作。

关于spring-boot - 如何实现动态@ConfigurationProperties 前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45557138/

相关文章:

spring-boot - 如何在属性文件中正确外部化 spring-boot kafka-streams 配置?

java - 使用 Mockito 和 jUnit5 的 Spring Boot Controller 测试失败,因为 Spring 无法创建加载属性的 Property Service 类的 bean?

java - 如何在Spring RestTemplate中实现批处理

java - 将 spring-boot 作为 java 应用程序启动时出现 list 错误

spring-mvc - 如何基于http方法隐藏相关端点

java - spring data elasticsearch聚合总和——价格、数量

java - REST 应用程序在 Eclipse 中运行,但不是作为可执行 jar。 NoClassDefFound错误: javax/xml/bind/annotation/XmlElement

java - 未使用自定义 DefaultTokenServices bean

spring-boot - SPRING_PROFILES_ACTIVE 和 Helm

java - 使用 Jackson/Springboot 解析可选 <T> 时出错