SpringBoot 加载yml文件自定义参数初始化问题

  •  有 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

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值