我有一個要求將動態環境名稱作爲配置屬性的前綴。我將通過命令行將環境作爲VM參數傳遞,並且應該爲該環境加載所有屬性。如何實現動態@ConfigurationProperties前綴
我的配置:
@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
問題是什麼? https://stackoverflow.com/help/how-to-ask – Brian
謝謝Brian。當我使用$ {environment}作爲@ConfigurationProperties的參數時,它不起作用。是否有任何技巧使用動態參數,我可以通過程序中的程序/ VM參數傳遞?我在Spring應用程序中做了同樣的事情,它工作正常,但在Spring引導的情況下,它不起作用。 – Saurabh