springboot demo实战项目

springboot SPEL demo实战

2019-07-22  本文已影响16人  灰色调诺言

简介

SPEL 是一种强大的表达式语言。在Spring产品组合中,它是表达式计算的基础。它支持在运行时查询和操作对象图,它可以与基于XML和基于注解的Spring配置还有bean定义一起使用。

说点人话:
SPEL表达式可集成数据库或者配置表实现简单的动态业务实现
SPEL表达式可用于日记的动态字段记录

Git地址

https://gitee.com/wqrzsy/lp-demo/tree/master/lp-springboot-spel

更多demo请关注

springboot demo实战项目
java 脑洞
java 面试宝典

项目分析

  1. maven 引入
 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
  1. 获取spring ioc 里面的bean,并调用bean方法
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("test", new Test());
        String result = spelExcetor.doneInSpringContext(dataMap, "@testController.test()");
  1. 获取指定属性名的参数,并调用方法
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("test", new Test());
        String result = spelExcetor.doneInSpringContext(dataMap, "#test.getName()");

4.其他,基本和在java写逻辑没啥区别了

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("test", new Test());
        String result = spelExcetor.doneInSpringContext(dataMap, "99 > 100 ? true : false");

PS:
用spel做业务功能,需要用流式的数据处理思想,举个栗子

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("test", new Test());
        String result = spelExcetor.doneInSpringContext(dataMap, "#test.getName().concat(\" world\").substring(0, 4)");

4. 测试

http://localhost:8080/swagger-ui.html

demo项目导入

参考: https://www.jianshu.com/p/cd0275a2f5fb

如果这篇文章对你有帮助请给个star


image.png
上一篇 下一篇

猜你喜欢

热点阅读