zoukankan      html  css  js  c++  java
  • Atitit java解析yml文件 以及使用 spel ognl 读取 v4 u77.docx Atitit java解析yml文件 目录 1.1. Springboot use snak

    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

     

     

     

     

      1. Springboot use snakeyaml

     

    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");

    }

     

    }

     

    1. 方式一:snakeyaml

    maven依赖添加

    1.  

    <dependency>

    1.  
    2.  

    <groupId>org.yaml</groupId>

    1.  
    2.  

    <artifactId>snakeyaml</artifactId>

    1.  
    2.  

    <version>1.10</version>

    1.  
    2.  

     

    1.  

     

     

    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

    }

     

      1. 多文档快的解决
      2. loadall

    在一个yaml文件中可以存入多组配置并使用loadAll进行读取,多组之间使用三个横杠分开

        @Test

        public void loadall() throws FileNotFoundException {

            Yaml yaml = new Yaml();

            File f = new File("test.yaml");

            Iterable<Object> result = yaml.loadAll(new FileInputStream(f));

            for (Object obj : result) {

                System.out.println(obj.getClass());

                System.out.println(obj);

            }

        }

    ----test.yaml---

     

      1. 问题解决

    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)

     

    多文档快的解决

     

      1. Code

    H:gitWorkSpace omcatxsrc omcatxpkgsonsyefen.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);

     

     

    1.   方式二:jyaml

    maven依赖添加

    1.  

    <dependency>

    1.  
    2.  

    <groupId>org.jyaml</groupId>

    1.  
    2.  

    <artifactId>jyaml</artifactId>

    1.  
    2.  

    <version>1.3</version>

    1.  
    2.  

     

    1.  
    1.  

     

     

    @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

  • 相关阅读:
    Winform 时间
    button的后台点击事件
    Winform文本框只能输入限定的文本
    vue的生命周期函数
    ES6新增语法
    购物车案例(JavaScript动态效果)
    前端es6总结
    jQuery与vue的区别是什么?
    vue实现双向绑定原理
    JS实现简单分页功能
  • 原文地址:https://www.cnblogs.com/attilax/p/15196905.html
Copyright © 2011-2022 走看看