- 有 application.yml 自定义参数如下:
mqconfig: bankCode: MQ mqServerURL: 192.168.0.111 mqServerPort: 1414 mqServerChannel: SERVER mqServerName: QMDWPB sendQueueName: DW_BB_F receiveQueueName: DW_BB_B remoteQueueName: DW_BB_F
- java 类使用注解
@ConfigurationProperties(prefix="mqconfig")
@Componen
@ConfigurationProperties(prefix="mqconfig")
@Component
public class MQConfig {
private String bankCode;
private String mqServerURL;
private int mqServerPort;
private String mqServerChannel;
private String mqServerName;
private String sendQueueName;
private String receiveQueueName;
private String remoteQueueName;
public String getBankCode() {
return bankCode;
}
public void setBankCode(String bankCode) {
this.bankCode = bankCode;
}
public String getMqServerURL() {
return mqServerURL;
}
public void setMqServerURL(String mqServerURL) {
this.mqServerURL = mqServerURL;
}
public int getMqServerPort() {
return mqServerPort;
}
public void setMqServerPort(int mqServerPort) {
this.mqServerPort = mqServerPort;
}
public String getMqServerChannel() {
return mqServerChannel;
}
public void setMqServerChannel(String mqServerChannel) {
this.mqServerChannel = mqServerChannel;
}
public String getMqServerName() {
return mqServerName;
}
public void setMqServerName(String mqServerName) {
this.mqServerName = mqServerName;
}
public String getSendQueueName() {
return sendQueueName;
}
public void setSendQueueName(String sendQueueName) {
this.sendQueueName = sendQueueName;
}
public String getReceiveQueueName() {
return receiveQueueName;
}
public void setReceiveQueueName(String receiveQueueName) {
this.receiveQueueName = receiveQueueName;
}
public String getRemoteQueueName() {
return remoteQueueName;
}
public void setRemoteQueueName(String remoteQueueName) {
this.remoteQueueName = remoteQueueName;
}
}
}
当我们启动springboot时候就可以得到数据了;
注意: application.yml 自定义参数不能大写,否则获取不到数据。
如下,这样是在spring boot启动时报错:
MQCONFIG:
bankCode: MQ
mqServerURL: 192.168.0.111
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MQController': Unsatisfied dependency expressed through field 'mqService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MQServiceImpl': Unsatisfied dependency expressed through field 'config'; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'MQConfig': Could not bind properties to 'MQConfig' : prefix=MQCONFIG, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.source.InvalidConfigurationPropertyNameException: Configuration property name 'MQCONFIG' is not valid