SpringBoot使用配置类映射yml配置文件信息

导入依赖

        
            org.springframework.boot
            spring-boot-configuration-processor
        

创建自己的配置映射类

使用configurationProperties注解并指明在配置文件中的前缀,然后用component注解注册进ioc容器。

我这里定义的两个静态内部类,同时创建了各自的实例,比如这个Email静态内部类中有三个属性,那么配置文件就可以是mq.myrabbit.email.queue来对静态内部类中的属性进行一个映射

/**
 * 配置映射类
 * @author houhaotong
 */
@Component
@ConfigurationProperties(prefix = "mq.myrabbit")
public class MyRabbitProperties {

    /** 过期时间 */
    private Long expire;

    private final Email email=new Email();

    private final Dead dead=new Dead();

    /** 发送邮件的消息模型 */
    public static class Email{

        /** 发送邮件队列名 */
        private String queue;

        /** 发送邮件交换机名 */
        private String exchange;

        /** 发送邮件routingKey */
        private String routingKey;

        public String getQueue() {
            return queue;
        }

        public void setQueue(String queue) {
            this.queue = queue;
        }

        public String getExchange() {
            return exchange;
        }

        public void setExchange(String exchange) {
            this.exchange = exchange;
        }

        public String getRoutingKey() {
            return routingKey;
        }

        public void setRoutingKey(String routingKey) {
            this.routingKey = routingKey;
        }
    }

    /** 死信消息模型(订单超时未支付) */
    public static class Dead{

        private String queue;

        private String exchange;

        private String routingKey;

        /** 真实队列 */
        private String realQueue;


        private String prodExchange;

        private String prodRoutingKey;

        public String getQueue() {
            return queue;
        }

        public void setQueue(String queue) {
            this.queue = queue;
        }

        public String getExchange() {
            return exchange;
        }

        public void setExchange(String exchange) {
            this.exchange = exchange;
        }

        public String getRoutingKey() {
            return routingKey;
        }

        public void setRoutingKey(String routingKey) {
            this.routingKey = routingKey;
        }

        public String getRealQueue() {
            return realQueue;
        }

        public void setRealQueue(String realQueue) {
            this.realQueue = realQueue;
        }

        public String getProdExchange() {
            return prodExchange;
        }

        public void setProdExchange(String prodExchange) {
            this.prodExchange = prodExchange;
        }

        public String getProdRoutingKey() {
            return prodRoutingKey;
        }

        public void setProdRoutingKey(String prodRoutingKey) {
            this.prodRoutingKey = prodRoutingKey;
        }
    }

    public Long getExpire() {
        return expire;
    }

    public void setExpire(Long expire) {
        this.expire = expire;
    }

    public Email getEmail() {
        return email;
    }

    public Dead getDead() {
        return dead;
    }
}

在yml中写配置
项目编译后,在yml中写配置会自动显示出来自己定义的配置映射类所具有的属性,如下图所示:


image.png

然后在yml配置文件中,写各个配置,配置类就会读取到了

mq:
  myrabbit:
    #邮件发送消息模型
    email:
      queue: seckill.email.queue
      exchange: seckill.email.exchange
      routing-key: seckill.email.routingKey
    dead:
      queue: seckill.dead.queue
      exchange: seckill.dead.exchange
      routing-key: seckill.dead.routingKey
    #设置过期时间
    expire: 1800000

你可能感兴趣的:(SpringBoot使用配置类映射yml配置文件信息)