yaml解析工具类(java)
创建时间: 2020年11月5日14:27:25
最后修改时间: 2020年11月5日17:35:40
简介
这是基于snakeyaml实现的一个读取yaml配置的工具类.
主要功能是获取指定层级之下的数据.
主要功能和使用方法
-
支持获取指定层级之后的对象数据,如"zdc.config" :
new ConfigBean().prefix(“zdc.config”).getT(A.class)
-
支持获取list中指定数据,如"zdc.config.list.1" :
new ConfigBean().prefix(“zdc.config.list.1”).getObj()
-
支持获取list中指定数据之后的数据,如"zdc.config.map.1.name"
new ConfigBean().prefix(“zdc.config.list.1”).getString()
`
具体代码
maven配置
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.25</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils-core</artifactId>
<version>1.8.3</version>
</dependency>
java代码
package zdc.utils;
import org.apache.commons.beanutils.BeanUtils;
import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
/**
* 读取resources下的yaml配置类
*
*使用的maven包
* <dependency>
* <groupId>org.yaml</groupId>
* <artifactId>snakeyaml</artifactId>
* <version>1.25</version>
* </dependency>
* map->bean使用此包
* <dependency>
* <groupId>commons-beanutils</groupId>
* <artifactId>commons-beanutils-core</artifactId>
* <version>1.8.3</version>
* </dependency>
*
*使用方法
*eg:
* zdc:
* config:
* key: value
* list:
* - 张三
* - 李四
* map:
* - name: 张三map
* age: 12
* - name: 李四map
* age: 121
*
*支持获取指定层级之后的数据,如"zdc.config" : new ConfigBean().prefix("zdc.config").getObj()
*
*支持获取list中指定数据,如"zdc.config.list.1" : new ConfigBean().prefix("zdc.config.list.1").getObj()
*
*支持获取list中指定数据之后的数据,如"zdc.config.map.1.name" :new ConfigBean().prefix("zdc.config.list.1").getString()
*
* @author ZDC
*/
public class ConfigBean {
/**
* 读取的资源名
*/
private String fileName ="application.yml";
/**
* 获取的对象
*/
private Object temp;
/**
* 创建一个资源获取对象,默认获取resources下的application.yml文件
*/
public ConfigBean() {
this.load();
}
/**
* 创建一个资源获取对象,默认获取resources下的fileName文件
* @param fileName
*/
public ConfigBean(String fileName) {
this.fileName =fileName;
this.load();
}
/**
* 加载指定的文件
*/
private ConfigBean load() {
Yaml yaml = new Yaml();
InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream(this.fileName);
this.temp= yaml.load(inputStream);
return this;
}
/**
* eg "zdc.config.list"
* eg ""
* @param prefix
*/
public ConfigBean prefix(String prefix){
if(prefix==null || "".equals(prefix.trim())){
return this;
}
//获取层级关机
String[] keys = prefix.trim().split("\\.");
for (String key : keys) {
//判断数据类型
if(this.temp instanceof Map){
this.temp= ((Map) this.temp).get(key);
}
else if(this.temp instanceof List){
if (isNumeric(key)) {
this.temp= ((List) this.temp).get(Integer.parseInt(key));
}else{
throw new RuntimeException(String.format("当前层级类型为List,不能使用[%s]获取子集数据",key));
}
}else{
throw new RuntimeException("暂时没有解析该类型或不支持再次解析");
}
}
return this;
}
/**
* 返回对象类型的数据,可能是List,Map,Obj
* @return
*/
public Object getObj(){
return this.temp;
}
/**
* 返回Map类型的数据
* @return
*/
public Map getMap() {
if(this.temp instanceof Map){
return (Map)this.temp;
}
return null;
}
/**
* 返回List类型的数据
* @return
*/
public List getList() {
if(this.temp instanceof List){
return (List)this.temp;
}
return null;
}
/**
* 返回对象类型数据,如果集成其他类库可以直接调用其他类库的map2bean方法 该处引用的是 commons-beanutils-core ,可以根据自己本地环境替换
*
* @param clazz
* @param <T>
* @return
*/
public <T> T getT(Class<T> clazz) throws Exception {
T obj = clazz.newInstance();
Map map = this.getMap();
BeanUtils.populate(obj,map);
return obj;
}
/**
* 返回String类型的数据
* @return
*/
public String getString() {
return this.temp==null ? "":this.temp.toString();
}
/**
* 返回Integer类型的数据
* @return
*/
public Integer getInteger() {
String string = getString();
return string!=null? Integer.parseInt(string):null;
}
//TODO 可以自定也解析其他类型
/**
* 判断是否是数字
* @param cs
* @return
*/
public static boolean isNumeric(final CharSequence cs) {
if (cs == null || cs.length() == 0) {
return false;
}
final int sz = cs.length();
for (int i = 0; i < sz; i++) {
if (!Character.isDigit(cs.charAt(i))) {
return false;
}
}
return true;
}
}