跳到主要内容

运算符

DeepSeek V3 中英对照 Operators

Spring 表达式语言支持以下类型的运算符:

关系运算符

关系运算符(等于、不等于、小于、小于或等于、大于、大于或等于)通过使用标准运算符符号来支持。这些运算符适用于 Number 类型以及实现了 Comparable 的类型。以下列表展示了一些关系运算符的示例:

// evaluates to true
boolean trueValue = parser.parseExpression("2 == 2").getValue(Boolean.class);

// evaluates to false
boolean falseValue = parser.parseExpression("2 < -5.0").getValue(Boolean.class);

// evaluates to true
boolean trueValue = parser.parseExpression("'black' < 'block'").getValue(Boolean.class);

// uses CustomValue:::compareTo
boolean trueValue = parser.parseExpression("new CustomValue(1) < new CustomValue(2)").getValue(Boolean.class);
java
备注

对于 null 的大于和小于比较遵循一个简单的规则:null 被视为无(即不是零)。因此,任何其他值总是大于 nullX > null 始终为 true),而没有其他值会小于无(X < null 始终为 false)。

如果你更喜欢数值比较,应避免基于数字的 null 比较,而是使用与零的比较(例如,X > 0X < 0)。

每个符号运算符也可以指定为纯文本等效形式。这样可以避免在表达式嵌入的文档类型(例如 XML 文档)中,所使用的符号具有特殊含义时引发的问题。文本等效形式如下:

  • lt (<)

  • gt (>)

  • le (<=)

  • ge (>=)

  • eq (==)

  • ne (!=)

所有文本操作符都是不区分大小写的。

除了标准的关系运算符外,SpEL 还支持 betweeninstanceof 和基于正则表达式的 matches 运算符。以下列表展示了所有三种运算符的示例:

boolean result;

// evaluates to true
result = parser.parseExpression(
"1 between {1, 5}").getValue(Boolean.class);

// evaluates to false
result = parser.parseExpression(
"1 between {10, 15}").getValue(Boolean.class);

// evaluates to true
result = parser.parseExpression(
"'elephant' between {'aardvark', 'zebra'}").getValue(Boolean.class);

// evaluates to false
result = parser.parseExpression(
"'elephant' between {'aardvark', 'cobra'}").getValue(Boolean.class);

// evaluates to true
result = parser.parseExpression(
"123 instanceof T(Integer)").getValue(Boolean.class);

// evaluates to false
result = parser.parseExpression(
"'xyz' instanceof T(Integer)").getValue(Boolean.class);

// evaluates to true
result = parser.parseExpression(
"'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);

// evaluates to false
result = parser.parseExpression(
"'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);
java
警告

between 操作符的语法是 <input> between {<range_begin>, <range_end>},这实际上是 <input> >= <range_begin> && <input> <= <range_end>} 的简写。

因此,1 between {1, 5} 的结果为 true,而 1 between {5, 1} 的结果为 false

警告

请注意原始类型,因为它们会立即被装箱为对应的包装类型。例如,1 instanceof T(int) 的值为 false,而 1 instanceof T(Integer) 的值为 true

逻辑运算符

SpEL 支持以下逻辑(布尔)运算符:

  • and (&&)

  • or (||)

  • not (!)

所有的文本运算符都是不区分大小写的。

以下示例展示了如何使用逻辑运算符:

// -- AND --

// evaluates to false
boolean falseValue = parser.parseExpression("true and false").getValue(Boolean.class);

// evaluates to true
String expression = "isMember('Nikola Tesla') and isMember('Mihajlo Pupin')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);

// -- OR --

// evaluates to true
boolean trueValue = parser.parseExpression("true or false").getValue(Boolean.class);

// evaluates to true
String expression = "isMember('Nikola Tesla') or isMember('Albert Einstein')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);

// -- NOT --

// evaluates to false
boolean falseValue = parser.parseExpression("!true").getValue(Boolean.class);

// -- AND and NOT --

String expression = "isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')";
boolean falseValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);
java

字符串操作符

你可以对字符串使用以下运算符。

  • 连接 (+)

  • 减法 (-)

    • 用于包含单个字符的字符串
  • 重复 (*)

以下示例展示了 String 运算符的使用:

// -- Concatenation --

// evaluates to "hello world"
String helloWorld = parser.parseExpression("'hello' + ' ' + 'world'")
.getValue(String.class);

// -- Character Subtraction --

// evaluates to 'a'
char ch = parser.parseExpression("'d' - 3")
.getValue(char.class);

// -- Repeat --

// evaluates to "abcabc"
String repeated = parser.parseExpression("'abc' * 2")
.getValue(String.class);
java

数学运算符

你可以在数字上使用以下运算符,并且会强制执行标准的运算符优先级。

  • 加法 (+)

  • 减法 (-)

  • 自增 (++)

  • 自减 (--)

  • 乘法 (*)

  • 除法 (/)

  • 取模 (%)

  • 指数幂 (^)

除法和取模运算符也可以指定为纯文本等效形式。这样可以避免在表达式中嵌入的文档类型(例如 XML 文档)中使用的符号具有特殊含义的问题。文本等效形式如下:

  • div (/)

  • mod (%)

所有文本运算符都是不区分大小写的。

备注

自增和自减运算符可以使用前缀(++A--A)或后缀(A++A--)表示法,适用于可以写入的变量或属性。

以下示例展示了数学运算符的使用:

Inventor inventor = new Inventor();
EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();

// -- Addition --

int two = parser.parseExpression("1 + 1").getValue(int.class); // 2

// -- Subtraction --

int four = parser.parseExpression("1 - -3").getValue(int.class); // 4

double d = parser.parseExpression("1000.00 - 1e4").getValue(double.class); // -9000

// -- Increment --

// The counter property in Inventor has an initial value of 0.

// evaluates to 2; counter is now 1
two = parser.parseExpression("counter++ + 2").getValue(context, inventor, int.class);

// evaluates to 5; counter is now 2
int five = parser.parseExpression("3 + ++counter").getValue(context, inventor, int.class);

// -- Decrement --

// The counter property in Inventor has a value of 2.

// evaluates to 6; counter is now 1
int six = parser.parseExpression("counter-- + 4").getValue(context, inventor, int.class);

// evaluates to 5; counter is now 0
five = parser.parseExpression("5 + --counter").getValue(context, inventor, int.class);

// -- Multiplication --

six = parser.parseExpression("-2 * -3").getValue(int.class); // 6

double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(double.class); // 24.0

// -- Division --

int minusTwo = parser.parseExpression("6 / -3").getValue(int.class); // -2

double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(double.class); // 1.0

// -- Modulus --

int three = parser.parseExpression("7 % 4").getValue(int.class); // 3

int oneInt = parser.parseExpression("8 / 5 % 2").getValue(int.class); // 1

// -- Exponential power --

int maxInt = parser.parseExpression("(2^31) - 1").getValue(int.class); // Integer.MAX_VALUE

int minInt = parser.parseExpression("-2^31").getValue(int.class); // Integer.MIN_VALUE

// -- Operator precedence --

int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(int.class); // -21
java

赋值运算符

要设置一个属性,可以使用赋值运算符(=)。这通常在调用 setValue 时完成,但也可以在调用 getValue 时进行。以下代码展示了两种使用赋值运算符的方式:

Inventor inventor = new Inventor();
EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();

parser.parseExpression("name").setValue(context, inventor, "Aleksandar Seovic");

// alternatively
String aleks = parser.parseExpression(
"name = 'Aleksandar Seovic'").getValue(context, inventor, String.class);
java

重载运算符

默认情况下,SpEL 的 Operation 枚举中定义的数学操作(ADDSUBTRACTDIVIDEMULTIPLYMODULUSPOWER)支持简单类型,如数字。通过提供 OperatorOverloader 的实现,表达式语言可以支持对其他类型进行这些操作。

例如,如果我们想要重载 ADD 运算符,以便可以使用 + 符号将两个列表连接起来,我们可以按照以下方式实现一个自定义的 OperatorOverloader

pubic class ListConcatenation implements OperatorOverloader {

@Override
public boolean overridesOperation(Operation operation, Object left, Object right) {
return (operation == Operation.ADD &&
left instanceof List && right instanceof List);
}

@Override
public Object operate(Operation operation, Object left, Object right) {
if (operation == Operation.ADD &&
left instanceof List list1 && right instanceof List list2) {

List result = new ArrayList(list1);
result.addAll(list2);
return result;
}
throw new UnsupportedOperationException(
"No overload for operation %s and operands [%s] and [%s]"
.formatted(operation, left, right));
}
}
java

如果我们在 StandardEvaluationContext 中将 ListConcatenation 注册为 OperatorOverloader,那么我们就可以像下面的示例中展示的那样,计算类似 {1, 2, 3} + {4, 5} 的表达式。

StandardEvaluationContext context = new StandardEvaluationContext();
context.setOperatorOverloader(new ListConcatenation());

// evaluates to a new list: [1, 2, 3, 4, 5]
parser.parseExpression("{1, 2, 3} + {2 + 2, 5}").getValue(context, List.class);
java
备注

OperatorOverloader 不会改变运算符的默认语义。例如,在上述示例中,2 + 2 仍然会计算为 4

警告

任何使用重载运算符的表达式都无法编译。详情请参阅编译器限制