springboot中对yaml文件的解析
一、YAML是“YAML不是一种标记语言”的外语缩写 (见前方参考资料原文内容);但为了强调这种语言以数据做为中心,而不是以置标语言为重点,而用返璞词重新命名。它是一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料序列的编程语言。
二、在springboot中的基础配置是通过yaml的方式来解析实现,当然也支持xml的文件解析。这里主要是学习源码中了解到yaml的解析过程。帮助自己在学习源码的时候,判定spring做了哪些操作。
三、yaml的配置文件
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/model?useUnicode=true&characterEncoding=utf-8
username: root
password: root
jpa:
show-sql: true
hibernate:
ddl-auto: update
四、yaml的解析过程
import org.springframework.core.io.Resource;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.reader.UnicodeReader; import java.io.IOException;
import java.util.*; public class YamlUtils { /**
* 单个yaml文件处理
* @param resource
* @return
* @throws IOException
*/
public static Map<String, Object> yamlHandler(@NonNull Resource resource) throws IOException {
//返回的结果
Map<String, Object> result = new LinkedHashMap<>();
//读取方式
UnicodeReader reader = new UnicodeReader(resource.getInputStream());
//单文件处理
Yaml yaml = new Yaml();
Object object = yaml.load(reader);
//这里只是简单处理,需要多个方式可以自己添加
if (object instanceof Map) {
Map map = (Map) object;
buildFlattenedMap(result, map, null);
}
reader.close();
return result;
} /**
* 单个yaml文件处理
* @param resources
* @return
* @throws IOException
*/
public static Map<String, Object> yamlHandler(@NonNull Resource[] resources) throws IOException { //返回的结果
Map<String, Object> result = new LinkedHashMap<>();
Yaml yaml = new Yaml();
//多个文件处理
Iterator<Resource> iterator = Arrays.stream(resources).iterator();
while (iterator.hasNext()) {
Resource resource = iterator.next();
UnicodeReader reader = new UnicodeReader(resource.getInputStream());
Object object = yaml.load(reader);
//这里只是简单处理,需要多个方式可以自己添加
if (object instanceof Map) {
Map map = (Map) object;
buildFlattenedMap(result, map, null);
}
reader.close();
}
return result;
} /**
* 这部分代码来至springboot源码部分对yaml的解析
* YamlProcessor.java buildFlattenedMap方法
* @param result
* @param source
* @param path
*/
private static void buildFlattenedMap(Map<String, Object> result, Map<String, Object> source, @Nullable String path) {
//循环读取原数据
source.forEach((key, value) -> {
//如果存在路径进行拼接
if (StringUtils.hasText(path)) {
if (key.startsWith("[")) {
key = path + key;
} else {
key = path + '.' + key;
}
}
//数据类型匹配
if (value instanceof String) {
result.put(key, value);
} else if (value instanceof Map) {
//如果是map,就继续读取
Map<String, Object> map = (Map)value;
buildFlattenedMap(result, map, key);
} else if (value instanceof Collection) {
Collection<Object> collection = (Collection)value;
if (collection.isEmpty()) {
result.put(key, "");
} else {
int count = 0;
Iterator var7 = collection.iterator(); while(var7.hasNext()) {
Object object = var7.next();
buildFlattenedMap(result, Collections.singletonMap("[" + count++ + "]", object), key);
}
}
} else {
result.put(key, value != null ? value : "");
}
});
} }
五、测试
public static void main(String[] args) throws IOException {
Map<String, Object> map = YamlUtils.yamlHandler(new ClassPathResource("config/application.yaml"));
System.out.println(map);
}

springboot中对yaml文件的解析的更多相关文章
- springboot中的pom文件是如何管理依赖的
我们来看一下新建完成后的springboot中的pom文件 <?xml version="1.0" encoding="UTF-8"?> <p ...
- apt系统中sources.list文件的解析
/etc/apt/sources.list 一般源信息都存在这个文件中.但众多软件源都放在一个文件中实在有点乱,于是新版ubuntu也有了分类的方法: 文件夹 /etc/apt/sources.li ...
- kubernetes中service yaml文件的port作用
yaml文件 [root@k8s-master ~]# cat service-hello.yaml apiVersion: v1 kind: Service metadata: name: serv ...
- 在ASP.NET MVC 框架中调用 html文件及解析get请求中的参数值
在ASP.NET MVC 框架中调用 html文件: public ActionResult Index() { using (StreamReader sr = new StreamReader(P ...
- Android从网络中获取xml文件并解析数据
public class XmlwebData { @SuppressLint("UseValueOf") public static List<Person> get ...
- springboot中访问jsp文件方式
首先,添加加载jsp文件的依赖包: <!--jsp依赖 对应springboot版本为2.1.4--><dependency> <groupId>org.apach ...
- android中init.rc文件的解析问题
init.rc中文件里会通过import /init.${ro.hardware}.rc文件,这个ro.hardware应该是某个详细的属性.而这个ro.hardware赋值应该是在Init进程中赋值 ...
- springboot不加载mapper文件问题解析
1. 场景描述 启动的时候报"springboot available: expected at least 1 bean which qualifies as autowire candi ...
- maven中的profile文件的解析
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
随机推荐
- mysql循环查询树状数据
完整function )) ) CHARSET utf8 BEGIN ) ; ) ; SET str = ''; SET cid =cast(rootId as CHAR); WHILE cid is ...
- php 关联数组遍历
<?php $age=array("); foreach($age as $x=>$x_value) { echo "Key=" . $x . ", ...
- python 进制转换
print hex(),hex(-) #转换成十六进制 print oct(),oct(-) #转换成八进制 print bin(),bin(-) #转换成二进制 print int("字面 ...
- python 单向链表
import sys import random class employee: def __init__(self): self.num= self.salary= self.name='' sel ...
- Nginx 多进程连接请求/事件分发流程分析
Nginx使用多进程的方法进行任务处理,每个worker进程只有一个线程,单线程循环处理全部监听的事件.本文重点分析一下多进程间的负载均衡问题以及Nginx多进程事件处理流程,方便大家自己写程序的时候 ...
- 理解 Git 的基本概念 ( Merging Collaborating Rebasing)
合并 Merging 在分支上开发新功能后,如何把新功能加入到主分支,让其它人得到你的修改呢?你需要使用命令 git merge 或 git pull. 这两个命令的语法如下: git merge [ ...
- js取整,保留小数位数、四舍五入、科学记数法及去掉数字末尾多余的0
1.向下取整 var num1 = 12.10345; var num2 =12.9801; var newnum1=Math.floor(num1) //结果 12 var newnum2= ...
- BZOJ1074 [SCOI2007]折纸origami
我们先看每个点可能从哪些点折过来的,2^10枚举对角线是否用到. 然后再模拟折法,查看每个点是否满足要求. 恩,计算几何比较恶心,还好前几天刚写过一道更恶心的计算几何,点类直接拷过来2333. /** ...
- DOM之概述
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 在JavaScript中进行文件处理,第一部分:基础
译注:原文是<JavaScript高级程序设计>的作者Nicholas Zakas写的,本翻译纯属为自己学习而做,仅供参考.原文链接:这里 很多年前,我在一次Goole面试被问到,如何在w ...