官方参考
特性概览
- 字符表达式
- 布尔和关系操作符
- 正则表达式
- 类表达式
- 访问properties,arrays,lists,maps
- 方法调用
- 赋值
- 调用构造器
- 三元操作符
- 变量
- 用户自定义函数
- 集合投影
- 集合选择
- 内联列表
- 模板表达式
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
字符表达式:
@Test
public void test01() { // 字符串表达式
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("new String('Hello, World').concat('!')");
System.out.println(exp.getValue());
}
@Test
public void test02() { // 字符串表达式
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'HelloWorld'.bytes.length");
int length= (Integer) exp.getValue();
System.out.println(length);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
布尔和关系表达式:
@Test
public void test03() { // 布尔和关系表达式
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext(new java.util.Date());
Expression exp = parser.parseExpression("#root.year + 1900 eq 2011");
boolean flag = exp.getValue(context, Boolean.class);
System.out.println(flag);
}
@Test
public void test04() { // 布尔和关系表达式
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext(new java.util.Date());
Expression exp = parser.parseExpression("!(#root.year + 1900 == 2011)");
boolean flag = exp.getValue(context, Boolean.class);
System.out.println(flag);
}
关系表达式语言中可以使用符号: ==, >=, <=, !, >, <
也可以使用eq, ge, le, not, gt, lt
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
正则表达式:
@Test
public void test05() { // 正则表达式
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'138-8000-4000' matches '\\d{3}-\\d{4}-\\d{4}'");
boolean flag = exp.getValue(Boolean.class);
System.out.println(flag);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
类表达式:
@Test
public void test06() { // 类表达式
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("T(java.util.UUID).randomUUID().toString().toUpperCase()");
String uuid = exp.getValue(String.class);
System.out.println(uuid);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
访问properties,arrays,lists,maps:
@Test
public void test07() { // 访问arrays
String[] array = new String[] {"马勒", "格", "彼得"};
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("array", array);
ExpressionParser parser = new SpelExpressionParser();
String value = parser.parseExpression("#array[0]").getValue(context, String.class);
System.out.println(value);
}
@Test
public void test08() { // 访问list
List<String> list = new ArrayList<String>(3);
list.add("马勒");
list.add("格");
list.add("彼得");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("list", list);
ExpressionParser parser = new SpelExpressionParser();
String value = parser.parseExpression("#list[1]").getValue(context, String.class);
System.out.println(value);
}
@Test
public void test09() { // 访问map
Map<String, String> map = new HashMap<String, String>();
map.put("first-name", "马勒");
map.put("middle-name", "格");
map.put("last-name", "彼得");
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("map", map);
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("#map['last-name']");
String value = exp.getValue(context, String.class);
System.out.println(value);
}
@Test
public void test09_2() { // 访问Properties
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext(System.getProperties());
Expression exp = parser.parseExpression("#root['user.dir']");
String userDir = exp.getValue(context, String.class);
System.out.println("user dir :" + userDir);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
方法调用:
@Test
public void test10() { // 方法调用
// 静态方法和非静态方法都可以用
String el = "T(java.util.Calendar).getInstance().getTimeInMillis()";
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(el);
String str = exp.getValue(String.class);
System.out.println(str);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
赋值表达式:
@Test
public void test11() { // 赋值
List<String> list = new ArrayList<String>();
list.add("");
list.add("");
list.add("");
StandardEvaluationContext context = new StandardEvaluationContext(list);
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("#root[2] = '123'");
String result = (String)exp.getValue(context);
System.out.println(result);
System.out.println(list.get(2));
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
三元表达式:
@Test
public void test14() { // 三元操作符
ExpressionParser parser = new SpelExpressionParser();
String result = parser.parseExpression("true?'love':'hate'").getValue(String.class);
System.out.println(result);
}
@Test
public void test15() { // 三元操作符
ExpressionParser parser = new SpelExpressionParser();
String name = parser.parseExpression("null?:'Unknown'").getValue(String.class);
System.out.println(name); // 'Unknown'
}
@Test
public void test16() { // 三元操作符
class User {
public String name;
};
EvaluationContext context = new StandardEvaluationContext(new User());
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("#root.name?.length()");
Integer length = exp.getValue(context, Integer.class);
System.out.println(length);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
使用变量:
@Test
public void test17() { // 变量
class User {
public String name;
};
User u = null;
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("user", u = new User());
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("#user.name = 'Malege Peter'");
String name = exp.getValue(context, String.class);
System.out.println(name);
System.out.println(u.name);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
用户自定义函数:
@Test
public void test18() throws SecurityException, NoSuchMethodException { // 用户自定义函数
class StringUtil {
public String reverse(CharSequence s) {
StringBuffer buffer = new StringBuffer(s);
return buffer.reverse().toString();
}
};
StandardEvaluationContext context = new StandardEvaluationContext(new StringUtil());
context.registerFunction("reverse", StringUtil.class.getDeclaredMethod("reverse", new Class<?>[]{CharSequence.class}));
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("reverse('Hello, World.')");
String str = exp.getValue(context, String.class);
System.out.println(str);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
集合的选择:
@Test
public void test19() { // 集合选择
int[] primes = {1,3,5,7,11,13,17,19,23};
EvaluationContext context = new StandardEvaluationContext(primes);
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("#root.?[#this le 11]");
List<Integer> list = exp.getValue(context, List.class);
System.out.println(list);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
集合的投影:
@Test
public void test20() { // 集合投影
class User {
public String name;
public String password;
public User(String n, String p) {
name = n; password = p;
}
};
Map<Integer, User> map = new HashMap<Integer, User>();
map.put(1, new User("zhang san", "1234"));
map.put(2, new User("li si", "12345"));
map.put(3, new User("wang wu", "****"));
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("map", map);
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("#map.![value.name]");
List<String> list = exp.getValue(context, List.class);
System.out.println(list);
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
内联列表:
@Test
@Test
public void test21() { // 内联列表
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("{{1,2,3,4}, {5,6,7,8}}");
List<List<Integer> > list = exp.getValue(List.class);
for (List<Integer> is : list) {
System.out.println(is);
}
}