我需要将动态环境名称作为配置属性的前缀传递。我将从命令行将环境作为 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/