首页 归档 关于 文件 Github
×

Yaml的配置文件读取

2022-02-14 17:56:39
J-Tools
  • Yaml
本文总阅读量(次):
本文字数统计(字):373
本文阅读时长(分):2

引入依赖

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.29</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>

配置文件

在项目resources下存在文件:config.yaml
内容如下:

1
2
3
4
5
code: 000
message: "成功"
stringList:
- "hello"
- "world"

只要定义同等名称及类型即可赋值获取

类YamlUtils

采用 @Data 注解 省略set/get 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

import lombok.Data;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
* author :demon
* date :2021 2021/8/15 14:51
* desc :读取yaml文件,并获取值
*/
@Data
public class YamlUtils {

private static YamlUtils yamlUtils;

public static YamlUtils getInstance() {
if (yamlUtils == null) {
yamlUtils = new YamlUtils();
}
return yamlUtils;
}

static {
try {
Yaml yaml = new Yaml();
InputStream inputStream = YamlUtils.class.getResourceAsStream("/config.yaml");
// 可以将值转换为Map,通过map我们取值就可以了.
Map<String, Object> ymlMap = yaml.load(inputStream);
// 动态赋值
yamlUtils = YamlUtils.class.newInstance();
for (Field field : yamlUtils.getClass().getDeclaredFields()) {
if (ymlMap.containsKey(field.getName())) {
boolean flag = field.isAccessible();
field.setAccessible(true);
Object object = ymlMap.get(field.getName());
if (object != null && field.getType().isAssignableFrom(object.getClass())) {
field.set(yamlUtils, object);
}
field.setAccessible(flag);
}
}

} catch (Exception e) {
e.printStackTrace();
}
}

private Integer code;
private String message;
private List<String> stringList;
}

测试

1
2
3
4
5
6
@Test
public void Test1(){
System.out.println(YamlUtils.getInstance().getCode());
System.out.println(YamlUtils.getInstance().getMessage());
System.out.println(YamlUtils.getInstance().getStringList());
}
完
Maven项目的插件使用
Maven多模块指定模块工程打包

本文标题:Yaml的配置文件读取

文章作者:十二

发布时间:2022-02-14 17:56:39

最后更新:2022-03-01 17:35:39

原始链接:https://www.zhuqiaolun.com/2022/02/1644832599629/1644832599629/

许可协议:署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

头像

十二

我想起那天夕阳下的奔跑,那是我逝去的青春。

分类

  • Blog4
  • ElasticSearch13
  • Git2
  • Go-FastDfs2
  • IDEA2
  • J-Package6
  • J-Tools21
  • Java2
  • JavaFx6
  • Kafka4
  • Linux2
  • Logger6
  • Maven5
  • MyBatis6
  • MyCat3
  • MySql2
  • Nginx5
  • OceanBase1
  • RabbitMq4
  • Redis6
  • SVN1
  • SpringBoot16
  • Tomcat6
  • WebService2
  • Windows2
  • kubernetes10

归档

  • 一月 20261
  • 十二月 20253
  • 八月 20252
  • 六月 20251
  • 二月 20251
  • 十二月 20244
  • 八月 202416
  • 六月 20241
  • 九月 20231
  • 八月 20231
  • 七月 20232
  • 八月 20222
  • 三月 202214
  • 二月 20224
  • 十一月 20211
  • 七月 20215
  • 六月 20213
  • 五月 20213
  • 四月 20211
  • 三月 202116
  • 二月 20212
  • 一月 20211
  • 十一月 202014
  • 十月 20201
  • 九月 202014
  • 八月 20205
  • 七月 20204
  • 六月 20208
  • 五月 20208

作品

我的微信 我的文件

网站信息

本站运行时间统计: 载入中...
本站文章字数统计:103.2k
本站文章数量统计:139
© 2026 十二  |  鄂ICP备18019781号-1  |  鄂公网安备42118202000044号
驱动于 Hexo  | 主题 antiquity  |  不蒜子告之 阁下是第个访客
首页 归档 关于 文件 Github