如何实现动态@ConfigurationProperties前缀

时间:2017-08-07 23:36:15

标签: 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

1 个答案:

答案 0 :(得分:0)

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

  

无法通过@PropertySource注释加载YAML文件。因此,如果您需要以这种方式加载值,则需要使用属性文件。

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