本文主要是介绍SpringBoot 加载yml文件自定义参数初始化问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 有 application.yml 自定义参数如下:
mqconfig:bankCode: MQmqServerURL: 192.168.0.111mqServerPort: 1414mqServerChannel: SERVERmqServerName: QMDWPBsendQueueName: DW_BB_FreceiveQueueName: DW_BB_BremoteQueueName: 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: MQmqServerURL: 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
这篇关于SpringBoot 加载yml文件自定义参数初始化问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!