Spring Expression Language(缩写为SpEL)是一种强大的表达式语言。在Spring 产品组合中,它是表达式计算的基础。它支持在运行时查询和操作对象图,它可以与基于 XML 和基于注解的 Spring 配置还有 bean 定义一起使用。由于它能够在运行时动态分配值,因此可以为我们节省大量Java代码。
依赖:
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>4.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.0.5.RELEASEversion>
dependency>
SpEL有三种用法,一种是在注解@Value中;一种是XML配置;最后一种是在代码块中使用Expression。
@Value("#{表达式}")
public String port;
<bean id="xxx" class="com.lizq.xxx">
<property name="port" value="#{表达式}">
bean>
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class SpelTest {
public static void main(String[] args) {
String skipExpress1 = "${value1==value2}";
String skipExpress2 = "${value1==value2 and value2 == value3}";
Map map = new HashMap<>();
map.put("value1", "val_1");
map.put("value2", "val_1");
map.put("value3", "val_3");
Boolean b1 = SpelTest.expressionParsing(skipExpress1, map);
System.out.println(b1);
Boolean b2 = SpelTest.expressionParsing(skipExpress2, map);
System.out.println(b2);
}
public static Boolean expressionParsing(String skipExpress, Map map) {
if (skipExpress != null && !"".equals(skipExpress) && map.isEmpty()) {
return false;
}
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
TemplateParserContext templateParserContext = new TemplateParserContext("${", "}");
PropertyAccessor propertyAccessor = new MapAccessor();
context.setVariables(map);
context.setPropertyAccessors(Arrays.asList(propertyAccessor));
SpelExpression expression = (SpelExpression) parser.parseExpression(skipExpress, templateParserContext);
expression.setEvaluationContext(context);
boolean value = expression.getValue(map, boolean.class);
return value;
}
}
<property name="userName" value="#{user.name}" />
<property name="userPrint" value="#{user.print()}" />
算术运算符:+,-,*,/,%,^
<property name="num" value="#{10^3}" />
<property name="yearMonth" value="#{2021+'年'+1+'月'}" />
比较运算符:<,>,==,<=,>=,lt,gt,eq,le,ge
逻辑运算符:and,or,not,&&,||,!
三目运算符:?true:false
正则表达式:matches
<property name="emailBool" value="#{email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}'}" />
通过 T() 调用一个类的静态方法,它将返回一个 Class Object,然后再调用相应的方法或属性:
<property name="PI" value="#{T(java.lang.Math).PI}" />
可以使用“#bean_id”来获取。有两个特殊的变量,可以直接使用。
String result2 = parser.parseExpression("#root").getValue(ctx, String.class);
String s = new String("获取容器内的变量");
ctx.setVariable("str",s);
//取id为abc的bean,然后调用其中的substring方法
parser.parseExpression("#str.substring(0,1)").getValue(ctx, String.class);
与Java代码没有什么区别,可见上面的例子
可以自定义方法,如下:
Method parseInt = Integer.class.getDeclaredMethod("parseInt", String.class);
ctx.registerFunction("parseInt1", parseInt);
ctx.setVariable("parseInt2", parseInt);
“registerFunction” 和 “setVariable” 都可以注册自定义函数,但是两个方法的含义不一样,推荐使用 “registerFunction” 方法注册自定义函数。
是三目运算符的特殊写法,可以避免null报错的情况
// name != null? name : "other" 简写为
name ? : "other"
为了避免操作对象本身可能为null,取属性时报错,定义语法
语法: “对象?.变量|方法”
list?.size()
此方法只能是java.lang 下的类才可以省略包名
Expression exp = parser.parseExpression("new Spring('Hello World')");
定义:使用“{表达式,……}”定义List,如“{1,2,3}”
访问:SpEL目前支持所有集合类型和字典类型的元素访问。语法:
“集合[索引]”、“map[key]”
修改:可以使用赋值表达式或Expression接口的setValue方法修改;
//赋值语句
int result = parser.parseExpression("#array[1] = 3").getValue(context, int.class);
//serValue方法
parser.parseExpression("#array[2]").setValue(context, 4);
选择:通过一定的规则对及格进行筛选,构造出另一个集合
语法:“(list|map).?[选择表达式]”
**注意**:选择表达式结果必须是boolean类型,如果true则选择的元素将添加到新集合中,false将不添加到新集合中
选择:根据集合中的元素中通过选择来构造另一个集合,该集合和原集合具有相同数量的元素
语法:“SpEL使用“(list|map).![投影表达式]”
// 从userlist下筛选出age>18的子集合,再将他们的name字段投为新的list
@Value("#{userlist.?[age>18].![name]}")
private ArrayList<String> usernames;
SpEL 支持使用“@”符号来引用 Bean,在引用Bean时需要使用 BeanResolver 接口实现来查找 Bean,Spring 提供 BeanFactoryResolver 实现;
public void testBeanExpression() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
ctx.refresh();
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(ctx));
Properties result1 = parser.parseExpression("@systemProperties").getValue(context, Properties.class);
Assert.assertEquals(System.getProperties(), result1);
}
参考文献:
https://www.jianshu.com/p/e0b50053b5d3