Spring SpEL 表达式

– Start
点击此处观看本系列配套视频。


在上节**Spring 配置数据库动态密码**中,我们使用 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);
	}

}

更多参见:Spring Framework 精萃
– 声 明:转载请注明出处
– Last Updated on 2017-05-30
– Written by ShangBo on 2017-05-30
– End


版权声明:本文为shangboerds原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。