读取复杂结构的yml配置项

1、yml配置项示例:(List的集合在第一项前面加 “-”)

rabbitmqsetting:
  exchangeList:
    - name: e1
      type: topic
      bindingList:
        - routingKey: r1
          queue: q1
        - routingKey: r2
          queue: q2
    - name: e2
      type: topic2
      bindingList:
        - routingKey: r3
          queue: q3
        - routingKey: r4
          queue: q4

2、添加解析配置项的jar

 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <version>2.0.4.RELEASE</version>
</dependency>

 

3、添加配置文件解析类(根据(1)分析数据结构创建实体类)

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;

@Component
@ConfigurationProperties("rabbitmqsetting")
public class RabbitMqSettingDate implements Serializable {

    private List<Exchange> exchangeList;

    public List<Exchange> getExchangeList() {
        return exchangeList;
    }

    public void setExchangeList(List<Exchange> exchangeList) {
        this.exchangeList = exchangeList;
    }

    @Override
    public String toString() {
        return "RabbitMqSettingDate{" +
                "exchangeList=" + exchangeList +
                '}';
    }
}

 

4、实体类的属性名要与配置文件中key相同

import java.io.Serializable;

public class Binding implements Serializable {

    private String routingKey;
    private String queue;

    public String getRoutingKey() {
        return routingKey;
    }

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

    public String getQueue() {
        return queue;
    }

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

    @Override
    public String toString() {
        return "Binding{" +
                "routingKey='" + routingKey + '\'' +
                ", queue='" + queue + '\'' +
                '}';
    }
}
import java.io.Serializable;
import java.util.List;

public class Exchange implements Serializable {

    private String name;
    private String type;
    private List<Binding> bindingList;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<Binding> getBindingList() {
        return bindingList;
    }

    public void setBindingList(List<Binding> bindingList) {
        this.bindingList = bindingList;
    }

    @Override
    public String toString() {
        return "Exchange{" +
                "name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", bindingList=" + bindingList +
                '}';
    }
}

5、使用封装好的数据(将最外层封装的实体类注入要使用该数据的类中)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class Test implements CommandLineRunner {
    @Autowired
    private RabbitMqSettingDate rabbitMqSettingDate;
    @Override
    public void run(String... args) throws Exception {
        System.out.println(rabbitMqSettingDate);
    }
}

 

 6、结果(System.out.println(data)手动格式化后的结果

 

posted @ 2018-08-14 15:08  品书读茶  阅读(337)  评论(0)    收藏  举报