Spring Expression Language (SpEL), like OGNL, is a powerful expression language that supports querying and manipulating an object graph at runtime.
To use SpEL, we need org.springframework.spring-expression jar.
Hello World
The interface EvaluationContext is used as a context for the expression. The out-of-the-box implementation, StandardEvaluationContext, uses reflection to manipulate the object.
Literal expression
The types of literal expressions supported are strings, dates, numeric values (int, real, and hex), boolean and null. Strings are delimited by single quotes. To put a single quote itself in a string use two single quote characters.
We firstly define a class to test:
In conf-spel.xml, we use #{expression} to indicate a SpEL. :
To test:
Variable
Like above hello world example. We could use variable by setVariable() function of EvaluationContext which means context for expression. Variables can be referenced in the expression using the syntax #variableName.
Xml-based Expression
**SpEL could reference the value of another bean by #{beanID} or #{@beanID}##. And as it’s an expression, we could add some other operations in it:
Annotation-based Expression
The @Value annotation can be placed on fields, methods and method/constructor parameters to specify a default value. So we could use SpEL to reference other beans to set default value.
Configuration file conf-spel.xml:
To test:
Function or constant in expressions
To invoke static function/constant in expression, we must know its class firstly by T(packagePath). Then call its function T(packagePath).Staticfunction(). In fact, the T operator is used to tell SpEL to deal with String inside T{} as class type.
T() references to types within java.lang do not need to be fully qualified, but all other type references must be.
To invoke class function, it’s the same as java language. If we already have an instance in SpEL, for example #{'HelloWorld'}, here as a String instance ,we could call all the string class function directly like this #{'HelloWorld'.function()}
Constructor in expressions
We could use new key word directly in SpEL, if class is not in java.lang, we need to specify the full package name:
Mathematical operators
Addition operator can be used on both numbers and strings.
Subtraction, multiplication and division can be used only on numbers.
Other mathematical operators supported are modulus (%) and exponential power (^).
Relational operators
There are two formats symbolic and textual. Textual format is suggested because it could avoid problems where the symbols used have special meaning for the document type in which the expression is embedded (eg. in XML).
lt (<)
gt (>)
le (<=)
ge (>=)
eq (==)
ne (!=)
Logic operators
or
and
not (!)
Elvis Operator
The Elvis operator is a shortening of the ternary operator syntax. It could avoid repeating a variable twice.
Same as:
Safe Navigation operator
Sometimes, we need to verify that an object is not null before accessing methods or properties of the object. The safe navigation operator will simply return null instead of throwing an exception.
Collection Selection
collection.?[condition_expression] will filter collection and return a new collection containing a subset of the original elements match conditions.
The following example selects from list cities all the cities which has more than 100000 populations:
Collection Projection
collection.!condition_expression] will create a new collection containing a subset which is the value of condition_expression.
The following example creates a new collection. Each of its item is a string combination of name and state from list cities:
Expression templating
Expression templates allow a mixing of literal text with one or more evaluation blocks. Each evaluation block is delimited with #{ }.