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)手动格式化后的结果

读取复杂结构的yml配置项的更多相关文章

  1. docker内程序如何读取dockerfile和compose.yml中设置的环境变量

    docker内程序如何读取dockerfile和compose.yml中设置的环境变量 背景 compose文件中配置了服务A和服务B,其中B服务调用了A服务的接口,那么B的实现代码中该如何调用A的服 ...

  2. spring boot 读取配置文件(application.yml)中的属性值

    在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值: 1.引入依赖: <!-- 支持 @ConfigurationProperties 注 ...

  3. springboot项目logback.xml或者logback-spring.xml中读取不到application.yml或application.properties配置文件中的配置解决办法

    在springboot项目中我们可能想要实现不同环境的日志项目配置不同,比如我想让不同环境的日志路径不同. 这时候我们很容易想: 1.到将日志路径配置在springboot的:application- ...

  4. 从oracle数据表中读取表结构

    drop table aa_tab;create table aa_tab asselect  A.TABLE_NAME  表名,  A.column_name 字段名,A.data_type  数据 ...

  5. sql server 读取表结构

    SELECT 表名 then d.name else '' end, 字段序号=a.colorder, 主键 FROM sysobjects where xtype='PK' and name in ...

  6. c# 怎么读取web.config中的配置项

    ConfigurationManager.AppSettings["templateId"]

  7. [转]SQL 读取表结构

    1.Sql SELECT column_name as FName,data_type as FType,CHARACTER_MAXIMUM_LENGTH as FLen from informati ...

  8. c语言学习之基础知识点介绍(十七):写入读取结构体、数组、结构体数组

    一.结构体的写入和读取 //写入结构体 FILE *fp = fopen("/Users/ios/Desktop/1.data", "w"); if (fp) ...

  9. C# 结构体和List<T>类型数据转Json数据保存和读取

    C#  结构体和List<T>类型数据转Json数据保存和读取 一.结构体转Json public struct FaceLibrary { public string face_name ...

随机推荐

  1. scull 中的设备注册

    在内部, scull 使用一个 struct scull_dev 类型的结构表示每个设备. 这个结构定义为: struct scull_dev { struct scull_qset *data;  ...

  2. https://webpack.js.org/plugins/

    有问题还是看源码 ,看官方文档吧,整一晚上终于整明白了

  3. html--垂直居中

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. Stopwatch 计时器类

    C#_Stopwatch 类   命名空间:System.Diagnostics Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 Stopwatc ...

  5. Java虚拟机性能管理神器 - VisualVM(7) 排查JAVA应用程序线程泄漏【转】

    Java虚拟机性能管理神器 - VisualVM(7) 排查JAVA应用程序线程泄漏[转] 标签: javajvm线程泄漏 2015-03-11 19:47 1098人阅读 评论(0) 收藏 举报   ...

  6. Linux中要重启apache服务与在windows是有很大的区别,下面我们来介绍一下

    在Linux中要重启apache服务与在windows是有很大的区别,下面我们来介绍一下常用的命令,需要的朋友参考下吧(http://www.hnkjlb.com) linux系统为Ubuntu 一. ...

  7. 高斯消元+期望dp——light1151

    高斯消元弄了半天没弄对.. #include<bits/stdc++.h> using namespace std; #define maxn 205 #define eps 1e-8 d ...

  8. Jtopo使用中link中文字与link平行

    修改源代码如下 //新增 a.translate(e ,f ); a.rotate(Math.atan((d.y-c.y)/(d.x-c.x))); //修改 a.fillText(this.text ...

  9. koa2 安装环境

    1.安装koa2 npm install koa@2 -g 2.创建项目 (1)安装koa2生成器 npm install koa-generator -g (2)koa2生成一个test项目 koa ...

  10. 在 Node.js 中引入模块:你所需要知道的一切都在这里

    本文作者:Jacob Beltran 编译:胡子大哈 翻译原文:http://huziketang.com/blog/posts/detail?postId=58eaf471a58c240ae35bb ...