当前位置:网站首页>SnakeYAML配置文件解析器
SnakeYAML配置文件解析器
2022-06-25 16:38:00 【四问四不知】
序言
实际开发项目中会有好多的配置文件,有yml后缀的配置文件,也有properties后缀的配置文件,xml、ini、conf、yaml等等。使用时大多都会经过加载解析的过程。
那么如果自己想要加载一个yaml文件的数据怎么做呢?
springboot框架中我们可以通过@Value("${}")注解加载配置文件中的对象、数组或纯量(scalars)。
下面看看如何使用SnakeYAML解析配置文件,
实战
创建一个test.yaml文件在resources目录下,
field1: "String"
field2: 2
properties:
name: "my Test"
age: 13
创建自己的实例对象类MyInstance
package com.hust.zhang.yaml;
import lombok.Data;
import java.util.Map;
@Data
public class MyInstance {
public String field1;
public Integer field2;
public Map<String, Object> properties;
}测试类
package com.hust.zhang.yaml;
import com.google.common.io.CharStreams;
import org.yaml.snakeyaml.Yaml;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class YamlTest {
//通过创建Yaml对象生成ThreadLocal初始化变量
private final static ThreadLocal<Yaml> YAML_THREAD_LOCAL = ThreadLocal.withInitial(Yaml::new);
public static void main(String[] args) {
String name = "/test.yaml";
MyInstance instance = load(name);
System.out.println(instance.getName());
}
/**
* 加载Yaml文件对象并转换成实例对象
*
* @param name
* @return
*/
private static MyInstance load(String name) {
Yaml yaml = YamlTest.YAML_THREAD_LOCAL.get();
try (final InputStream inputStream = Yaml.class.getResourceAsStream(name)) {
final String conf = CharStreams.toString(new InputStreamReader(inputStream));
return yaml.loadAs(conf, MyInstance.class);
} catch (IOException e) {
System.out.println("read yaml error : " + e);
} catch (Exception e) {
System.out.println("other error :" + e);
}
return null;
}
}输出结果如下,

上面需注意的点:
1、ThreadLocal是一个绑定线程的变量,它让每个线程独享自己的副本,避免了资源竞争。这样每个线程调用都能够拿到自己的Yaml对象。你说直接在主线程里创建Yaml对象不可以吗?当然可以,只不过这么写是为了复习ThreadLocal知识点。
2、try-catch语句块中用到了try-with-resource语法,这个前面讲过可以不需要我们手动去释放IO资源,简化了代码。
3、通过class.getResourceAsStream(String name)方法获取输入流,查询资源规则由给定的类的类加载器实现,对name以"/"开头读取绝对路径的资源。而它和class.getClassLoader().getResourceAsStream(String name)方法区别在于,后者是获取当前类的类加载器,默认从resources文件中获取不带"/"开头的资源。通过new InputStream(new File(name))获取输入流资源也是可以的。
4、yaml.loadAs()方法可以从输入流中获取数据转换成对象,也可以从String字符串中获取数据转换成对象。
参考链接:
边栏推荐
猜你喜欢

Xshell connecting VMware virtual machines

1-8file sharing in VMWare

mysql使用过程中遇到的问题

论文笔记:LBCF: A Large-Scale Budget-Constrained Causal Forest Algorithm

Day_ twelve

XXIX - orbslam2 real-time 3D reconstruction using realsensed435

Using pywebio testing, novice testers can also make their own testing tools

内卷?泡沫?变革?十个问题直击“元宇宙”核心困惑丨《问Ta-王雷元宇宙时间》精华实录...

STM32硬件错误HardFault_Handler的处理方法

Redis系列——概述day1-1
随机推荐
剑指 Offer II 035. 最小时间差
STM32硬件错误HardFault_Handler的处理方法
MySQL_ JDBC
Kalman Filter 遇到 Deep Learning : 卡尔曼滤波和深度学习有关的论文
The problem of missing precision of kettle table input components
APIJSON简单使用
A complete collection of APP testing tools. It's enough to collect this one
从TiDB上线阿里云的背后,如何看待云数据库的变革趋势
Kettle表输入组件精度丢失的问题
组件通讯的方式有哪些
居家办公让我绩效拿了C | 社区征文
Perfect shuffle problem
mysql使用过程中遇到的问题
Internship: the annotation under swagger involves the provision of interfaces
Kalman filter meets deep learning: papers on Kalman filter and deep learning
WPF开发随笔收录-心电图曲线绘制
Tasklet API usage
mac php多版本管理以及安装swoole扩展
Read mysql45 lecture - index
FreeRTOS内核时钟不对的问题解决