Atitit java解析yml文件 以及使用 spel ognl 读取 v4 u77.docx
Atitit java解析yml文件
目录
1.1. Springboot use snakeyaml 1
2. 方式一:snakeyaml 2
2.1. 多文档快的解决 3
2.2. loadall 3
2.3. 问题解决 4
2.4. Code 4
3. 方式二:jyaml 5
package miniCodePrjPkg;
import org.checkerframework.checker.units.qual.Speed;//
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
//import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.yaml.snakeyaml.Yaml;
import com.google.common.collect.Maps;
//@SpringBootApplication
public class YmlUtil {
public static void main(String[] args) {
//Maps.newLinkedHashMap()
Yaml yaml = new Yaml();
Object ret = yaml.load(YmlUtil.class.getClassLoader()
.getResourceAsStream("bootstrap.yml"));
System.out.println(ret);
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context3 = new StandardEvaluationContext();
context3.setVariable("map8", ret);
Object result3 = new SpelExpressionParser().parseExpression("#map8['spring']['profiles']['active']").getValue(context3);
System.out.println(result3);
System.out.println("f");
}
}
maven依赖添加
<dependency>
<groupId>org.yamlgroupId>
<artifactId>snakeyamlartifactId>
<version>1.10version>
private static void ymlSingledoc(String ymlString) throws FileNotFoundException {
// ymlString="H:\\gitWorkSpace\\tomcatx\\t.yml";
org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml();
// Object mObject=yaml.load(sonsyefen.class.getResourceAsStream("/test.yml"));
Object mObject = yaml.load(new FileInputStream(ymlString));
System.out.println(JSON.toJSONString(mObject, true));
// TestEntity testEntity =
// yaml.loadAs(sonsyefen.class.getResourceAsStream("/test.yml"),
// TestEntity.class);//如果读入Map,这里可以是Mapj接口,默认实现为LinkedHashMap
}
在一个yaml文件中可以存入多组配置并使用loadAll进行读取,多组之间使用三个横杠分开
@Test
public void loadall() throws FileNotFoundException {
Yaml yaml = new Yaml();
File f = new File("test.yaml");
Iterablenew FileInputStream(f));
for (Object obj : result) {
System.out.println(obj.getClass());
System.out.println(obj);
}
}
----test.yaml---
Exception in thread "main" expected a single document in the stream
in 'reader', line 1, column 1:
server:
^
but found another document
in 'reader', line 36, column 1:
---
^
at org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:111)
at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:140)
at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:524)
at org.yaml.snakeyaml.Yaml.load(Yaml.java:452)
at tomcatxpkg.sonsyefen.main(sonsyefen.java:26)
多文档快的解决
H:\gitWorkSpace\tomcatx\src\tomcatxpkg\sonsyefen.java
多文档快
Map m = YmlUtil.getDoc(ymlString, new Predicate<Map>() {
@Override
public boolean test(Map m) {
// can use ognl improve
// Map spring = (Map) t.get("spring");
// if (spring.get("profiles").equals("test"))
// return true;
try {
Object expression = Ognl.parseExpression("spring.profiles");
Object value = Ognl.getValue(expression, m);
if(value==null)
return false;
if(value.equals("test"))
return true;
} catch (OgnlException e) {
e.printStackTrace();
//if cont contain this key ,then continue
//throw new RuntimeException(e);
}
return false;
}
});
// 非根节点取值需要#开头
Object expression = Ognl.parseExpression("spring.datasource");
Object value = Ognl.getValue(expression, m); // Ognl.getValue(expression);
System.out.println(value);
maven依赖添加
<dependency>
<groupId>org.jyamlgroupId>
<artifactId>jyamlartifactId>
<version>1.3version>
@Test
public void testJyml() {
TestEntity testEntity = null;
try {
testEntity = org.ho.yaml.Yaml.loadType(DemoApplicationTests.class.getResourceAsStream("/test.yml"), TestEntity.class);//如果是读入Map,这里不可以写Ma接口,必须写实现
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(testEntity);
---------------------
TestEntity{age=418, name='Jack', params={event=what's up, url=http://www.test.com}, favoriteBooks=[Gone with the wind, The Little Prince]}
Java使用snakeyaml解析yaml - resentment - 博客园.mhtml
(9+条消息)java解析yml文件 - 张林强的专栏 - CSDN博客.html