博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring SpEL 表达式
阅读量:4050 次
发布时间:2019-05-25

本文共 6376 字,大约阅读时间需要 21 分钟。

– Start


在上节****中,我们使用 SpEL 表达式声明 bean,现在让我们来看看它的语法。

实例化基本类型对象

package shangbo.spring.spel.example1;import org.springframework.expression.ExpressionParser;import org.springframework.expression.spel.standard.SpelExpressionParser;public class App {	public static void main(String[] args) {		ExpressionParser parser = new SpelExpressionParser();				// 使用单引号定义字符串		String strObj = parser.parseExpression("'Hello World'").getValue(String.class);		System.out.println(strObj);				// 定义整数		int intObj = parser.parseExpression("1").getValue(Integer.class);		System.out.println(intObj);				// 定义浮点数		double doubleObj = parser.parseExpression("1.0").getValue(Double.class);		System.out.println(doubleObj);				// 定义布尔型		boolean booleanObj = parser.parseExpression("true").getValue(Boolean.class);		System.out.println(booleanObj);				// 定义 null		Object nullValue = parser.parseExpression("null").getValue();		System.out.println(nullValue);	}}

实例化集合类型对象

package shangbo.spring.spel.example2;import java.util.List;import java.util.Map;import org.springframework.expression.ExpressionParser;import org.springframework.expression.spel.standard.SpelExpressionParser;public class App {	public static void main(String[] args) {		ExpressionParser parser = new SpelExpressionParser();				// 定义数组		Integer[] intArray = (Integer[]) parser.parseExpression("new Integer[] {1, 2, 3, 4}").getValue();		System.out.println(intArray);				// 定义 List		List listObj = parser.parseExpression("{1,2,3,4}").getValue(List.class);		System.out.println(listObj);				// 定义 Map		Map mapObj = parser.parseExpression("{'zhangsan':28,'lsi':30}").getValue(Map.class);		System.out.println(mapObj);				// 访问对象属性, #this 代表当前对象, #root 代表根对象		Integer arrayLen = parser.parseExpression("#this.length").getValue(intArray, Integer.class);		System.out.println(arrayLen);				// 根据 index 访问 list 元素		int num = parser.parseExpression("#this[3]").getValue(listObj, Integer.class);		System.out.println(num);				// .?[selectionExpression] 过滤 list		List subListObj = (List) parser.parseExpression("#this.?[#this < 3]").getValue(listObj);		System.out.println(subListObj);				// 根据 key 访问 map		int age = parser.parseExpression("#this['zhangsan']").getValue(mapObj, Integer.class);		System.out.println(age);	}}

实例化普通对象

package shangbo.spring.spel.example3;import org.springframework.expression.ExpressionParser;import org.springframework.expression.spel.standard.SpelExpressionParser;import org.springframework.expression.spel.support.StandardEvaluationContext;import shangbo.spring.spel.Inventor;import shangbo.spring.spel.StringUtils;public class App {	public static void main(String[] args) throws Exception {		ExpressionParser parser = new SpelExpressionParser();				// 通过构造函数初始化对象		String helloWorld = parser.parseExpression("new String('hello world')").getValue(String.class);		System.out.println(helloWorld);		// 调用方法		String c = parser.parseExpression("substring(2, 3)").getValue(helloWorld, String.class);		System.out.println(c);				// 调用静态方法		double r = parser.parseExpression("T(java.lang.Math).random()").getValue(Double.class);		System.out.println(r);				// 定义对象		Inventor inventor = parser.parseExpression("new shangbo.spring.spel.Inventor()").getValue(Inventor.class);				// 赋值方式 1		parser.parseExpression("name").setValue(inventor, "Shangbo");		System.out.println(inventor.getName());				// 注册变量		StandardEvaluationContext context = new StandardEvaluationContext(inventor);		context.setVariable("newName", "Scott");				// 赋值方式 2, #newName 引用新变量		parser.parseExpression("Name = #newName").getValue(context);		System.out.println(inventor.getName());				// 注册方法		context.registerFunction("reverseString", StringUtils.class.getDeclaredMethod("reverseString", new Class[] { String.class }));		// 调用注册方法		String helloWorldReversed = parser.parseExpression("#reverseString('hello')").getValue(context, String.class);		System.out.println(helloWorldReversed);	}}

操作符

package shangbo.spring.spel.example4;import org.springframework.expression.ExpressionParser;import org.springframework.expression.spel.standard.SpelExpressionParser;public class App {	public static void main(String[] args) {		ExpressionParser parser = new SpelExpressionParser();		// 关系操作符		System.out.println(parser.parseExpression("1 == 1").getValue(Boolean.class));		System.out.println(parser.parseExpression("1 != 1").getValue(Boolean.class));		System.out.println(parser.parseExpression("1 > 1").getValue(Boolean.class));		System.out.println(parser.parseExpression("1 >= 1").getValue(Boolean.class));		System.out.println(parser.parseExpression("1 < 1").getValue(Boolean.class));		System.out.println(parser.parseExpression("1 <= 1").getValue(Boolean.class));				// 为了在 XML 中是使用方便,也可以使用如下方式		System.out.println(parser.parseExpression("1 eq 1").getValue(Boolean.class));		System.out.println(parser.parseExpression("1 ne 1").getValue(Boolean.class));		System.out.println(parser.parseExpression("1 gt 1").getValue(Boolean.class));		System.out.println(parser.parseExpression("1 ge 1").getValue(Boolean.class));		System.out.println(parser.parseExpression("1 lt 1").getValue(Boolean.class));		System.out.println(parser.parseExpression("1 le 1").getValue(Boolean.class));				// 逻辑操作符		System.out.println(parser.parseExpression("true and false").getValue(Boolean.class));		System.out.println(parser.parseExpression("true or false").getValue(Boolean.class));		System.out.println(parser.parseExpression("!true").getValue(Boolean.class));				// 数学运算符		System.out.println(parser.parseExpression("1 + 1").getValue(Integer.class));		System.out.println(parser.parseExpression("1 - 1").getValue(Integer.class));		System.out.println(parser.parseExpression("1 * 1").getValue(Integer.class));		System.out.println(parser.parseExpression("1 / 1").getValue(Integer.class));		System.out.println(parser.parseExpression("1 % 1").getValue(Integer.class));				// instanceof 操作符		// T() 表示一个类, 非 java.lang 包下的类需要包名		System.out.println(parser.parseExpression("'xyz' instanceof T(Integer)").getValue(Boolean.class));				// matches操作符, 匹配正则表达式		System.out.println(parser.parseExpression("'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class));				// If-Then-Else		String falseString = parser.parseExpression("false ? 'trueExp' : 'falseExp'").getValue(String.class);		System.out.println(falseString);	}}

– 声 明:转载请注明出处
– Last Updated on 2017-05-30
– Written by ShangBo on 2017-05-30
– End

你可能感兴趣的文章
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>