Spel

前言:此文章主要解決你不能使用jdk8處理集合的情況還有一些模板的使用,使用字符串調(diào)用方法
1.模板替換

    @Test
    public void  test7()  {
        ExpressionParser parser = new SpelExpressionParser();
        EvaluationContext ctx = new StandardEvaluationContext();
        ctx.setVariable("date",new Date());
        ParserContext parserContext = new ParserContext() {
            @Override
            public boolean isTemplate() {
                return true;
            }
            @Override
            public String getExpressionPrefix() {
                return "{";
            }
            @Override
            public String getExpressionSuffix() {
                return "}";
            }
        };
        String template = "現(xiàn)在時(shí)間:    {#date}";
        Expression expression = parser.parseExpression(template, parserContext);
        log.info(expression.getValue(ctx,String.class));
    }

result:

[10:03:53][INFO ][AppTest][line:133]:現(xiàn)在時(shí)間:    Wed Dec 18 10:03:53 GMT+08:00 2019

2.操作集合

   @Test
    public void  test9()  {
         List<SpObject> primes = new ArrayList< >();
        primes.addAll(Arrays.asList(new SpObject(0,"000"),new SpObject(1,"111")));

         ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setVariable("primes",primes);
 
        List<SpObject> primesGreaterThanTen =
                 parser.parseExpression("#primes.?[#this.code>0]").getValue(context,List.class);
        log.info(primesGreaterThanTen.toString());
    }



    public static class SpObject{
        private int code;
        private String name;

        public SpObject(int code, String name) {
            this.code = code;
            this.name = name;
        }

        public int getCode() {
            return code;
        }

        public String getName() {
            return name;
        }

result:

[10:06:46][INFO ][AppTest][line:163]:[SpObject{code=1, name='111'}]
        @Test
    public void  test10()  {
         List<Map<String,Object>> primes = new ArrayList< >();
        for (int i = 0; i < 10; i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("code",i);
            map.put("name",i+"");
            primes.add(map);
        }
         ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setVariable("primes",primes);

        List<Map<String,Object>> primesGreaterThanTen =
                 parser.parseExpression("#primes.?[#this[code]>5]").getValue(context,List.class);
        log.info(primesGreaterThanTen.toString());

        context.setVariable("list",primesGreaterThanTen);
        Map<String,Object> value = parser.parseExpression("#list.?[#this[code]>0][0]").getValue(context, Map.class);
        log.info(value.toString());

        context.setVariable("list",primesGreaterThanTen);
        List str = parser.parseExpression("#list.![#this[code]]").getValue(context, List.class);
        log.info(str.toString());
    }

result:

[10:28:59][INFO ][AppTest][line:180]:[{code=6, name=6}, {code=7, name=7}, {code=8, name=8}, {code=9, name=9}]
[10:28:59][INFO ][AppTest][line:184]:{code=6, name=6}
[10:28:59][INFO ][AppTest][line:188]:[6, 7, 8, 9]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容