Operators in Spring Expression Language

SpEL offers several operations that you can apply on values in a SpEL expression.

Table 1.1 SpEL Operators
Operation type Operators
Arithmetic +, -, *, /, %, ^
Relational <, >, ==, <=, >=, lt, gt, eq, le, ge
Logical and, or, not, |
Conditional ?: (ternary), ?: (Elvis)
Regular expression matches

Arithmetic Operators

SpEL supports all of the basic arithmetic operators that Java supports, plus the carat (^) operator for performing a power of operation. The addition operator can be used on numbers, strings and dates. Subtraction can be used on numbers and dates. Multiplication and division can be used only on numbers. Standard operator precedence is enforced.

For example, to add two numbers together, the + operator can be used like this:

<property name="addResult" value="#{number.numberA + number.numberB}" />

You can overload + operator to perform concatenation on String values like this:

<property name="name" value="#{employee.firstName + ' ' + employee.lastName}" />

Click Here for source code.

Relational Operators

SpEL supports all of the comparison operators that Java supports.

For example: you can use the == operator to compare two numbers:

<property name="equal" value="#{number.A == number.B}" />

SpEL also supports the less-than (<) , greater-than (>) , operatorsgreater-than-or-equals (>=) and the less-than-or-equals (<=) operators. Less-than and Greater-than have special meaning in XML, so using them in expression causes problem while using them in XML Configuration. So it is advised to use SpEL’s textual alternatives. For example:

<property name="greater" value="#{number.A gt number.B}" />

Table 1.2 SpEL Relational Operator and their textual alternatives
Operation Symbol Symbol
Equals == eq
Less than < lt
Less than or equals <= le
Greater than > gt
Greater than or equals >= Greater than or equals

Logical Operators

SpEL supports all of the logical operators that Java supports. For example:

<property name="expressionValue" value="#{number.A gt 10 and number.A lt 100}" />

You can use either symbolic ! operator or the textual not operator to negate a Boolean expresson. For example:

<property name="expressionValue" value="#{!number.A gt 10}" />
<property name="expressionValue" value="#{not number.A gt 10}" />

Conditional Operators

SpEL’s ternary operator (condition ? true : false) works the same as Java’s ternary operator. For example:

<propertyname="expression" value="#{number.A % 2 == 0 ? 'Even' : 'Odd'}"/>

A common use of the ternary operator is to check for a null value and to wire a default value in the event of a null. For example:

<propertyname="expression" value="#{number.A != null ? number.A : 0}"/>

In above code, you can see there is a bit duplication in number.A. So SpEL offers a variant of the ternary operator that simplifies this expression: i.e. Elvis Operator (?:)

For example:

<propertyname="expression" value="#{number.A ?: 0}"/>

Regular expression

SpEL supports pattern matching in expressions with its matches operator. The matches operator attempts to apply a regular expression against a String value. The result of a matches evaluation is a Boolean value: true if the value matches the regular expression, false otherwise. For example:

<property name="validEmail" value="#{employee.email matches'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}"/>
<property name="validDigit" value="#{'9' matches '\d+' }" />

, , ,

No comments yet.

Leave a Reply