读取复杂结构的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)手动格式化后的结果

读取复杂结构的yml配置项的更多相关文章
- docker内程序如何读取dockerfile和compose.yml中设置的环境变量
docker内程序如何读取dockerfile和compose.yml中设置的环境变量 背景 compose文件中配置了服务A和服务B,其中B服务调用了A服务的接口,那么B的实现代码中该如何调用A的服 ...
- spring boot 读取配置文件(application.yml)中的属性值
在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值: 1.引入依赖: <!-- 支持 @ConfigurationProperties 注 ...
- springboot项目logback.xml或者logback-spring.xml中读取不到application.yml或application.properties配置文件中的配置解决办法
在springboot项目中我们可能想要实现不同环境的日志项目配置不同,比如我想让不同环境的日志路径不同. 这时候我们很容易想: 1.到将日志路径配置在springboot的:application- ...
- 从oracle数据表中读取表结构
drop table aa_tab;create table aa_tab asselect A.TABLE_NAME 表名, A.column_name 字段名,A.data_type 数据 ...
- sql server 读取表结构
SELECT 表名 then d.name else '' end, 字段序号=a.colorder, 主键 FROM sysobjects where xtype='PK' and name in ...
- c# 怎么读取web.config中的配置项
ConfigurationManager.AppSettings["templateId"]
- [转]SQL 读取表结构
1.Sql SELECT column_name as FName,data_type as FType,CHARACTER_MAXIMUM_LENGTH as FLen from informati ...
- c语言学习之基础知识点介绍(十七):写入读取结构体、数组、结构体数组
一.结构体的写入和读取 //写入结构体 FILE *fp = fopen("/Users/ios/Desktop/1.data", "w"); if (fp) ...
- C# 结构体和List<T>类型数据转Json数据保存和读取
C# 结构体和List<T>类型数据转Json数据保存和读取 一.结构体转Json public struct FaceLibrary { public string face_name ...
随机推荐
- java.nio.file.FileSystemException: D:\kafka_2.12-2.1.0\kafka_2.12-2.1.0\logs\__consumer_offsets-30\00000000000000000000.timeindex.cleaned: 另一个程序正在使用此文件,进程无法访问。
在启动kafka时候报错: java.nio.file.FileSystemException: D:\kafka_2.12-2.1.0\kafka_2.12-2.1.0\logs\__consume ...
- (转)Lua语言实现简单的多线程模型
转自: https://blog.csdn.net/john_crash/article/details/49489609 lua本身是不支持真正的多线程的,但是lua提供了相应的机制来实现多线程.l ...
- 04.Mybatis输出映射之ResultMap
当实体类中的字段名与数据库中的字段名不一致时需要手动设置映射关系 在Mapper.xml中定义 <!-- resultMap最终还是要将结果映射到pojo上,type就是指定映射到哪一个pojo ...
- CF755F PolandBalls and Gifts
题意:给你一个礼物的置换.有k个人忘带了礼物.一个人无法获得礼物为他自己没有带礼物或给他带礼物的那个人没有带礼物.求选择k个人,没有获得礼物的人数的最小值和最大值. n,k<=1e6. 标程: ...
- Lock方法是用于数据库的锁机制,
Lock方法是用于数据库的锁机制,如果在查询或者执行操作的时候使用: lock(true); 复制代码 就会自动在生成的SQL语句最后加上 FOR UPDATE或者FOR UPDATE NOWAI ...
- React弹窗组件
原文地址 小寒的博客 这里的弹窗泛指所有的弹出组件,这些组件不受页面其他UI布局影响,处于DOM结构的顶层,绝对定位在body元素下. 这个特殊性也给它的开发提出了特殊的要求. react新版本中的c ...
- linux 软件 手动添加至桌面或启动栏
1.创建软连接(也可以不用创建软连接,直接写绝对路径) sudo ln -s /opt/eclipse/eclipse /usr/bin/eclipse 2.创建desktop文件 sudo gedi ...
- Java-JPA:JPA
ylbtech-Java-JPA:JPA JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对 ...
- 软件-版本控制器-VisualSVN:VisualSVN
ylbtech-软件-版本控制器-VisualSVN:VisualSVN VisualSVN 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 0. https:/ ...
- 03. 将pdb调试文件包含到.vsix包中
vs插件如何把pdb文件打包进去,方便记录日志和调试 <PropertyGroup> <CopyLocalLockFileAssemblies>true</CopyLoc ...