Spel

前言:此文章主要解决你不能使用jdk8处理集合的情况还有一些模板的使用,使用字符串调用方法
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 = "现在时间:    {#date}";
        Expression expression = parser.parseExpression(template, parserContext);
        log.info(expression.getValue(ctx,String.class));
    }

result:

[10:03:53][INFO ][AppTest][line:133]:现在时间:    Wed Dec 18 10:03:53 GMT+08:00 2019

2.操作集合

   @Test
    public void  test9()  {
         List 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 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> primes = new ArrayList< >();
        for (int i = 0; i < 10; i++) {
            Map 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> primesGreaterThanTen =
                 parser.parseExpression("#primes.?[#this[code]>5]").getValue(context,List.class);
        log.info(primesGreaterThanTen.toString());

        context.setVariable("list",primesGreaterThanTen);
        Map 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]

你可能感兴趣的:(Spel)