• spring 小示例 yongqi


    package springel;
    
    
    import ch.qos.logback.classic.spi.STEUtil;
    import lombok.Getter;
    import lombok.Setter;
    import lombok.extern.slf4j.Slf4j;
    import org.assertj.core.util.Lists;
    import org.assertj.core.util.Maps;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.expression.Expression;
    import org.springframework.expression.ExpressionParser;
    import org.springframework.expression.spel.standard.SpelExpressionParser;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.expression.spel.support.StandardEvaluationContext;
    import org.springframework.expression.EvaluationContext;
    import org.springframework.expression.common.TemplateParserContext;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    
    /**
     * @Author: Jemmy
     * @Date: 2022/1/7 15:18
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @Slf4j
    
    public class SpringELTest {
        @Test
        public void test1() throws Exception{
            //表达式解析器
            ExpressionParser parser = new SpelExpressionParser();
            // 设置表达式
            Expression exp = parser.parseExpression("1.024E+3");
            //String str = (String) exp.getValue();
            Object str =  exp.getValue();
            log.info("str:{}",str);
        }
        @Test
        public void test2() throws Exception{
            //表达式解析器
            ExpressionParser parser = new SpelExpressionParser();
            // 设置表达式
            Expression exp = parser.parseExpression("1.024E+3");
            Object str=exp.getValue(Long.class);
            log.info("str:{}",str);
        }
        @Test
        public void test3() throws Exception{
            //表达式解析器
            // String template ="#{#user},早上好";
            ExpressionParser paser = new SpelExpressionParser();
            Expression expression = paser.parseExpression("123");
            Object str= expression.getValue(Integer.class);
            log.info("str:{}",str);
        }
    
        @Test
        public void test4() throws Exception{
            //表达式解析器
            String name = "Tom";
            EvaluationContext context = new StandardEvaluationContext();  // 表达式的上下文,
            context.setVariable("myName", name);                 // 为了让表达式可以访问该对象, 先把对象放到上下文中
            ExpressionParser parser = new SpelExpressionParser();// 访问变量
            Object str =parser.parseExpression("#myName+',Hello'").getValue(context, String.class);   // Tom , 使用变量
    // 直接使用构造方法创建对象
            Object str1= parser.parseExpression("new String('aaa')").getValue(String.class);   // aaa
            Object str2= parser.parseExpression("'gian_test'").getValue(String.class);   // aaa
            log.info("str:{}-----{}-----{}",str,str1,str2);
        }
    
        @Test
        public void test5() throws Exception{
            //表达式解析器
            String name = "Tom";
            String name1="gina_test";
            EvaluationContext context = new StandardEvaluationContext();  // 表达式的上下文,
            context.setVariable("myName1", name1);
            context.setVariable("myName",name);
            context.setVariable("end","!");
            //context.setVariable("myName", name1,"myName",name);   // 为了让表达式可以访问该对象, 先把对象放到上下文中
            ExpressionParser parser = new SpelExpressionParser();// 访问变量
            Object str =parser.parseExpression("#myName").getValue(context, String.class);   // Tom , 使用变量
    // 直接使用构造方法创建对象
            Object str1= parser.parseExpression("new String('aaa')").getValue(String.class);   // aaa
            Object str2= parser.parseExpression("'gian_test'").getValue(String.class);   // aaa
            Object str3=parser.parseExpression("('Hello' + ' World').concat(#end)").getValue(context);
            //Object str4=parser.parseExpression("(#myName).concat(#myName1)").getValue(context, String.class);
            Object str4=parser.parseExpression("#myName+#myName1+#end").getValue(context, String.class);
            Object str5=parser.parseExpression("('Hello World')+#end").getValue(context);
            log.info("str:{}****{}****{}****{}****{}***{}",str,str1,str2,str3,str4,str5);
            // log.info("str:{}",str);
        }
    
        class Person{
            @Getter
            @Setter
            String name;
            @Getter
            @Setter
            Integer age;
            public Person(String name, Integer age) {
                this.name = name;
                this.age = age;
            }
        }
        @Test
        public void test6() throws Exception{
            // 准备工作
            Person person = new Person("Tom", 18); // 一个普通的POJO
            //List<String> list = Lists.newArrayList("a", "b");
            List<String> list =new ArrayList<String>();
            list.add("a");
            list.add("b");
            list.add("d");
            list.add("d");
            Map<String, Integer> map = new HashMap<String,Integer>();
            map.put("A", 1);
            map.put("B", 2);
            /*
            Map<String, String> map = new HashMap<String,String>();
            map.put("A", "1");
            map.put("B", "2");
            */
            EvaluationContext context = new StandardEvaluationContext(map);  // 表达式的上下文,
            context.setVariable("person", person);
            //context.setVariable("map", map);
            context.setVariable("list", list);
            ExpressionParser parser = new SpelExpressionParser();
    // 属性
            parser.parseExpression("#person.name").getValue(context, String.class);       // Tom , 属性访问
            parser.parseExpression("#person.Name").getValue(context, String.class);       // Tom , 属性访问, 但是首字母大写了
    // 列表
            Object e_list=parser.parseExpression("#list[0]").getValue(context, String.class)  ;         // a , 下标
    // map
           // Object e_map=parser.parseExpression("#map[A]").getValue(context, String.class);           // 1 , key
    // 方法
            Object str = parser.parseExpression("#person.getAge()").getValue(context, Integer.class); // 18 , 方法访问
            log.info("str:{}-----{}-----{}",e_list,e_map,str);
        }
    
    
        @Test
        public void test7() throws Exception{
            //表达式解析器
            // 设置表达式
            //Expression exp = parser.parseExpression("1.024E+3");
            String name11 = "Tom";
            Person person = new Person("Tom", 18); // 一个普通的POJO
            ExpressionParser parser=new SpelExpressionParser();// 访问变量
            EvaluationContext context = new StandardEvaluationContext();  // 表达式的上下文,
            context.setVariable("name11",name11);
            context.setVariable("person",person);
            parser.parseExpression("1 > -1").getValue(Boolean.class);         // true
            parser.parseExpression("1 gt -1").getValue(Boolean.class);        // true
            parser.parseExpression("true or true").getValue(Boolean.class);   // true
            parser.parseExpression("true || true").getValue(Boolean.class);   // true
            parser.parseExpression("2 ^ 3").getValue(Integer.class);          // 8
            parser.parseExpression("true ? true : false").getValue(Boolean.class); // true
            Object str1=parser.parseExpression("#name ?: 'default'").getValue(context, String.class); // default
            parser.parseExpression("1 instanceof T(Integer)").getValue(Boolean.class); // true
            //Object str=parser.parseExpression("'5.0' matches '^-?\\\\d+(\\\\.\\\\d{2})?$'").getValue(Boolean.class); // true
            Object str2=parser.parseExpression("#person.name").getValue(context, String.class);  // Tom , 原来的值
            parser.parseExpression("#person.name = 'Jim'").getValue(context, String.class); // Jim , 赋值之后
            parser.parseExpression("#person.name").getValue(context, String.class);  // Jim, 赋值起了作用
            log.info("str:-----{}-----{}",str1,str2);
        }
    
        @Test
        public void test8() throws Exception {
            //表达式解析器
            // String template ="#{#user},早上好";
            ExpressionParser parser = new SpelExpressionParser();
            EvaluationContext context = new StandardEvaluationContext();  // 表达式的上下文,
            context.setVariable("name", "123");
            Object str = parser.parseExpression("#name?.toUpperCase()").getValue(context, String.class); // null
            //Expression expression = paser.parseExpression("123");
            //Object str= expression.getValue(Integer.class);
            log.info("str:{}", str);
        }
    
        @Test
        public void test10() throws Exception {
            //表达式解析器
            // String template ="#{#user},早上好";
            ExpressionParser parser = new SpelExpressionParser();
            EvaluationContext context = new StandardEvaluationContext();  // 表达式的上下文,
            context.setVariable("name", "123");
            Object str = parser.parseExpression("#name?.toUpperCase()").getValue(context, String.class); // null
            //Expression expression = paser.parseExpression("123");
            //Object str= expression.getValue(Integer.class);
            log.info("str:{}", str);
        }
    
        @Test
        public void test9() throws Exception {
            //表达式解析器
            // String template ="#{#user},早上好";
            ExpressionParser parser = new SpelExpressionParser();
            Object str=parser.parseExpression("{1, 3, 5, 7}.?[#this > 3]").getValue(); // [5, 7]
            log.info("str:{}", str);
        }
        @Test
        public void test11() throws Exception {
            //表达式解析器
            // 集合
            ExpressionParser parser=new SpelExpressionParser();
            parser.parseExpression("{1, 3, 5,4, 7}.?[#this > 3]").getValue(); // [5, 7] , 选择元素
            Object str=parser.parseExpression("{1, 3, 5,4, 7}.^[#this > 3]").getValue(); // 5 , 第一个
            parser.parseExpression("{1, 3, 5, 6,7}.$[#this > 3]").getValue(); // 7 , 最后一个
            parser.parseExpression("{1, 3, 5,9, 7}.![#this + 1]").getValue(); // [2, 4, 6, 8] ,每个元素都加1
    
    // map
            Map<Integer, String> map = new HashMap<Integer, String>();
            map.put(1, "A");
            map.put(2, "B");
            map.put(3, "C");
            map.put(4, "D");
            map.put(54, "D");
            EvaluationContext context = new StandardEvaluationContext();
            context.setVariable("map", map);
            parser.parseExpression("#map.?[key > 3]").getValue(context);             // {4=D}
            parser.parseExpression("#map.?[value == 'A']").getValue(context);        // {1=A}
            Object str1=parser.parseExpression("#map.[key > 2 and key < 100]").getValue(context); // {3=C}
             //parser.parseExpression("#map.![#this+20]").getValue(context); // {3=C}
            log.info("str:{}", str1);
    
        }
    
        @Test
        public void test12() throws Exception {
            //表达式解析器
            String template = "#{#user},早上好";
            ExpressionParser parser = new SpelExpressionParser();
            EvaluationContext context = new StandardEvaluationContext();
            context.setVariable("user","LELE");
            Object str1= parser.parseExpression(template,new TemplateParserContext()).getValue(context,String.class);
            Object str2=parser.parseExpression("#{#user!=null?'他的名字叫'+#user+'先生':'不知道名字'}", new TemplateParserContext()).getValue(context);
            log.info("str{}***{}*", str1,str2);
        }
    }
    

      

  • 相关阅读:
    Vue 插件写法
    js创建对象的多种方式及优缺点
    webpack原理与实战
    发布高性能迷你React框架anu
    Windows 同一时候开启核心显卡与独立显卡(不接显示器启动核芯显卡)
    基于QT和OpenCV的人脸检測识别系统(1)
    Cocos2d-x中背景音乐播放暂停与继续
    使用Xcode和Instruments调试解决iOS内存泄露
    Shell 命令行快捷键
    ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展(实例)
  • 原文地址:https://www.cnblogs.com/gina11/p/15785362.html
Copyright © 2020-2023  润新知