使用SPEL自定义表达式
2022-05-28 00:09
1151 查看
自定义表达式
Spring提供了一个可以自定义表达式的接口
package com.qbb.qmall.item; import org.junit.Test; import org.springframework.expression.Expression; import org.springframework.expression.common.TemplateParserContext; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; import java.util.Arrays; /** * @author QiuQiu&LL (个人博客:https://www.cnblogs.com/qbbit) * @version 1.0 * @date 2022-05-27 23:33 * @Description: */ public class ExpressionTest { @Test public void test01() { // 准备一个表达式解析器 SpelExpressionParser spelExpressionParser = new SpelExpressionParser(); // 定义一个表达式字符串 String spelString = "qiuqiu&#{1+21}";// 计算表达式中的数据 // 创建一个模板表达式解析器上下文 TemplateParserContext parserContext = new TemplateParserContext(); // 解析表达式 Expression expression = spelExpressionParser.parseExpression(spelString, parserContext); // 计算表达式,获取结果 StandardEvaluationContext context = new StandardEvaluationContext(); context.setVariable("args", Arrays.asList(12,13,14,15)); // 计算上下文,动态计算结果,返回值为字符串 String value = expression.getValue(context, String.class); System.out.println("value = " + value); } }
- 修改字符串表达式继续测试
String spelString = "qiuqiu&#{#args[1]}";// 获取args的第二个参数
- 修改字符串表达式继续测试
String spelString = "qiuqiu&#{#args[1] * #args[2]}";// 获取args的第二个参数
- 修改字符串表达式继续测试
String spelString = "qiuqiu&#{#args[1] * #args[2]}:#{new String('spel').toUpperCase()}";// 调用方法
- 修改字符串表达式继续测试
String spelString = "qiuqiu&#{#args[1] * #args[2]}:#{new String('spel').toUpperCase()}" + "===#{T(java.lang.Math).random()}";// 调用方法
- 修改字符串表达式继续测试
String spelString = "当前时间:#{#currentDate}";
详细的使用信息,各位小伙伴可以参考官方文档
相关文章推荐
- Excel自定义函数使用正则表达式详解
- Spring使用SpEL表达式装配Bean
- Spring3.x--使用SpEL表达式装配
- spring_scope作用域和使用外部属性文件和spEL表达式
- 使用SpEL表达式装配Bean
- spring ioc容器注入的集中方式,以及属性注入的集中依赖,继承,使用SPEL表达式,ioc容器的生命周期,bean的几种工厂方法配置方式,bean的自动装配
- [Spring实战系列](11)SpEL使用表达式装配
- 使用Jquery.Validate通过正则表达式自定义验证规则汇总(常用验证规则),非常实用
- SpEL表达式使用记录
- python里使用string.Template实现自定义转义字符和正则表达式替换
- 使用SpEL表达式来获取SpringData Jpa在更新数据时传递的对象参数的属性
- perl 自定义包/模块的使用(正则表达式匹配email地址和ip)
- LINQ to SQL 第九节 在控件中使用自定义的LINQ表达式(翻译自scott的博客)
- 7.12.1: spel---使用expression接口进行表达式求值
- Spring4 学习系列之——SpEl表达式的使用
- spring同时使用aop和spel表达式出错:Property or field 'instrument' cannot be found on object of type 'com.sun.pr
- Spring 使用表达式进行装配 SpEL表达式
- spring同时使用aop和spel表达式出错:Property or field 'instrument' cannot be found on object of type 'com.sun.pr
- 大叔的第三天@ 正则表达式~使用自定义标题~屏幕适配~edittext的边框~一些常量
- Spring Cache抽象-使用SpEL表达式