• SpEl简单使用


    SpEl: spring expression language : spring 表达式语言,是一种强大的表达式语言

    虽然 SpEl 是 spring 产品组合中表达式评估的基础,但它不直接与 spring绑定,可以独立使用

    1.创建SpelExpressionParser 解析器
    2. 解析表达式 获取 Expression 对象
    3. 设置上下文环境 StandardEvaluationContext
    4. 获取值

    在这里插入图片描述

    代码:

        // eval 执行一个表达式   javascript
            String expl  = "1+2";
            SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
            Expression expression = spelExpressionParser.parseExpression(expl);
            // 获取表达式运行结果
            Object value = expression.getValue();
            System.out.println("value = " + value);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    获取类的值:

       User user = new User(1, "waterkid", "beijing");
    
            String spel = "#user.address";
            SpelExpressionParser parser = new SpelExpressionParser();
            Expression expression = parser.parseExpression(spel);
            // 让 解析器 知道  将user解析到 表达式中
            StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext();
            standardEvaluationContext.setVariable("user",user);
            Object value = expression.getValue(standardEvaluationContext);
            // 根据这个 环境 去获取 user 的值
            System.out.println("value = " + value);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
      User user = new User(1, "waterkid", "beijing");
            String spel = "address";
            SpelExpressionParser parser = new SpelExpressionParser();
            Expression expression = parser.parseExpression(spel);
            StandardEvaluationContext context = new StandardEvaluationContext();
            context.setRootObject(user);
            System.out.println(expression.getValue(context));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
      User user = new User(1, "waterkid", "beijing");
            SpelExpressionParser parser = new SpelExpressionParser();
            String name = parser.parseExpression("username").getValue(new StandardEvaluationContext(), user, String.class);
            System.out.println("name = " + name);
    
    • 1
    • 2
    • 3
    • 4

    获取方法返回值:

      String spel = "#userService.sayHello('silly b')";
            SpelExpressionParser parser = new SpelExpressionParser();
            Expression expression = parser.parseExpression(spel);
            StandardEvaluationContext context = new StandardEvaluationContext();
            context.setVariable("userService",new UserService());
            System.out.println("-------");
            Object value = expression.getValue(context);
            System.out.println("value = " + value);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    获取spring容器bean:如果 评估上下文 已经配置了 bean的解析器,可以使用@符号从表达式中查找 bean

    // spring 中  BeanFactory 可以直接引入
       @Autowired
        BeanFactory beanFactory;
        
        @Test
        public void test03(){
            String spel = "@userService.sayHello('silly b')";
            SpelExpressionParser parser = new SpelExpressionParser();
            Expression expression = parser.parseExpression(spel);
            StandardEvaluationContext context = new StandardEvaluationContext();
            // 解析 bean
            context.setBeanResolver(new BeanFactoryResolver(beanFactory));
            System.out.println(expression.getValue(context));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    HTTP不同版本之间的关系和区别
    【IoT】如何快速了解一个行业?如何做市场洞察?
    操作系统之虚拟内存总结
    wzsc_文件上传(条件竞争)
    CF1201C最大中位数题解
    Hive 和 HDFS、MySQL 之间的关系
    『NLP学习笔记』TextCNN文本分类原理及Pytorch实现
    【Kubernetes中Gateway和ServiceEntry使用、SDS认证授权等使用】
    windows安装多个版本jdk
    刚学习网格布局写一个计算器模型
  • 原文地址:https://blog.csdn.net/qq_36022463/article/details/126194919