Spring Expression Language (SpEL) 是 Spring 框架中用于处理表达式的语言,它支持从 Java 5 开始的 lambda 表达式,并具有类似 XPath 和 SQL 的语法。SpEL 提供了丰富的运算符,可以用于处理和操作对象图
以下是 SpEL 的一些主要运算符:
#root: 它代表当前表达式正在处理的根对象。在复杂的表达式中,可以用它来导航到根对象。.: 用于访问对象的属性或方法。例如,"person.name"表示访问 person 对象的 name 属性。如果需要访问嵌套的对象,可以使用连续的.,例如 "person.address.city"。[]: 用于访问集合和数组的元素。例如,"persons[0]" 表示访问 persons 集合的第一个元素。它还可以配合 #root 使用,例如 "#root[0]"。(): 用于调用方法。例如,"person.getName()" 表示调用 person 对象的 getName() 方法。+ 和 -: 用于加法和减法。* 和 /: 用于乘法和除法。%: 用于求模。^: 用于计算幂。! 和 !!: 用于逻辑非和严格非。第一个 ! 将转化为 Java 的 ! 运算符,而 !! 将创建一个新的空对象或 null 值,取决于被操作的上下文。?: 和 ??: 用于空值处理和空值合并。例如,"persons?.name ?? 'unknown'" 表示如果 persons 不为 null,则返回 persons 的 name,否则返回 “unknown”。== 和 !=: 用于比较两个值是否相等或不相等。< 和 >: 用于比较两个值的大小关系。<= 和 >=: 用于比较两个值的大小关系是否满足条件。and 和 or: 用于逻辑与和逻辑或。not: 用于逻辑非。matches: 用于检查一个字符串是否满足某个正则表达式,或者一个对象是否匹配某个条件。例如,"person.name matches 'John.*'" 表示检查 person 的 name 是否以 John 开头。下面是一个 SpEL 的简单示例:
假设我们有一个 person 对象,包含 name 和 age 两个属性和一个 getName() 方法:
class Person {
private String name;
private int age;
public String getName() {
return name;
}
// getters and setters...
}
那么我们可以这样使用 SpEL:
@Autowired
private ExpressionParser parser;
public void evaluatePerson() {
Person person = new Person();
person.setName("John");
person.setAge(30);
Expression exp = parser.parseExpression("name"); // 获取 name 属性
String name = (String) exp.getValue(person); // 在 person 对象上求值并获取结果
System.out.println(name); // 输出 "John"
exp = parser.parseExpression("age + 10"); // 计算 age+10 的值
int newAge = (int) exp.getValue(person); // 在 person 对象上求值并获取结果
System.out.println(newAge); // 输出 "40"
exp = parser.parseExpression("getName()"); // 调用 getName() 方法
String personName = (String) exp.getValue(person); // 在 person 对象上求值并获取结果
System.out.println(personName); // 输出 "John"
}
package com.yiibai.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("customerBean")
public class Customer {
//Relational operators
@Value("#{1 == 1}") //true
private boolean testEqual;
@Value("#{1 != 1}") //false
private boolean testNotEqual;
@Value("#{1 < 1}") //false
private boolean testLessThan;
@Value("#{1 <= 1}") //true
private boolean testLessThanOrEqual;
@Value("#{1 > 1}") //false
private boolean testGreaterThan;
@Value("#{1 >= 1}") //true
private boolean testGreaterThanOrEqual;
//Logical operators , numberBean.no == 999
@Value("#{numberBean.no == 999 and numberBean.no < 900}") //false
private boolean testAnd;
@Value("#{numberBean.no == 999 or numberBean.no < 900}") //true
private boolean testOr;
@Value("#{!(numberBean.no == 999)}") //false
private boolean testNot;
//Mathematical operators
@Value("#{1 + 1}") //2.0
private double testAdd;
@Value("#{'1' + '@' + '1'}") //1@1
private String testAddString;
@Value("#{1 - 1}") //0.0
private double testSubtraction;
@Value("#{1 * 1}") //1.0
private double testMultiplication;
@Value("#{10 / 2}") //5.0
private double testDivision;
@Value("#{10 % 10}") //0.0
private double testModulus ;
@Value("#{2 ^ 2}") //4.0
private double testExponentialPower;
@Override
public String toString() {
return "Customer [testEqual=" + testEqual + ", testNotEqual="
+ testNotEqual + ", testLessThan=" + testLessThan
+ ", testLessThanOrEqual=" + testLessThanOrEqual
+ ", testGreaterThan=" + testGreaterThan
+ ", testGreaterThanOrEqual=" + testGreaterThanOrEqual
+ ", testAnd=" + testAnd + ", testOr=" + testOr + ", testNot="
+ testNot + ", testAdd=" + testAdd + ", testAddString="
+ testAddString + ", testSubtraction=" + testSubtraction
+ ", testMultiplication=" + testMultiplication
+ ", testDivision=" + testDivision + ", testModulus="
+ testModulus + ", testExponentialPower="
+ testExponentialPower + "]";
}
}
package com.yiibai.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("numberBean")
public class Number {
@Value("999")
private int no;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
}
执行
Customer obj = (Customer) context.getBean("customerBean");
System.out.println(obj);
输出结果
Customer [testEqual=true, testNotEqual=false, testLessThan=false, testLessThanOrEqual=true, testGreaterThan=false, testGreaterThanOrEqual=true, testAnd=false, testOr=true, testNot=false, testAdd=2.0, testAddString=1@1, testSubtraction=0.0, testMultiplication=1.0, testDivision=5.0, testModulus=0.0, testExponentialPower=4.0]
请参阅在XML文件定义bean的等效版本。在XML中,类似 < 小于符号不支持,应该使用下面所示文本形式,例如文本等值, (‘<’ = ‘lt’) 和 (‘<=’ = ‘le’).
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="customerBean" class="com.yiibai.core.Customer">
<property name="testEqual" value="#{1 == 1}" />
<property name="testNotEqual" value="#{1 != 1}" />
<property name="testLessThan" value="#{1 lt 1}" />
<property name="testLessThanOrEqual" value="#{1 le 1}" />
<property name="testGreaterThan" value="#{1 > 1}" />
<property name="testGreaterThanOrEqual" value="#{1 >= 1}" />
<property name="testAnd" value="#{numberBean.no == 999 and numberBean.no lt 900}" />
<property name="testOr" value="#{numberBean.no == 999 or numberBean.no lt 900}" />
<property name="testNot" value="#{!(numberBean.no == 999)}" />
<property name="testAdd" value="#{1 + 1}" />
<property name="testAddString" value="#{'1' + '@' + '1'}" />
<property name="testSubtraction" value="#{1 - 1}" />
<property name="testMultiplication" value="#{1 * 1}" />
<property name="testDivision" value="#{10 / 2}" />
<property name="testModulus" value="#{10 % 10}" />
<property name="testExponentialPower" value="#{2 ^ 2}" />
bean>
<bean id="numberBean" class="com.yiibai.core.Number">
<property name="no" value="999" />
bean>
beans>