Spring Expression Language SpEL

Spring Expression Language (SpEL) is a powerful expression language that supports querying and manipulating an object graph at runtime.

This tutorial shows various examples of ways to use SpEL expressions with XML and annotation based configuration. We also know how to evaluate an expression using SpEL classes and interfaces.

I. Overview

In both cases: XML based configuration and Annotation based configuration, the syntax to define the expression is: #{ expression.. }

1. XML based configuration

2. Annotation based configuration

3. Expression Evaluation

SpEL classes and interfaces are located in the packages org.springframework.expression. We parse an expression string by using interface ExpressionParser and evaluate it by using interface Expression.

We can catch a ParseException when calling parser.parseExpression() or a EvaluationException when calling exp.getValue().

II. Functionality
– mathematical, logical, relational or ternary operators

We can also use XML based configuration for cases in which annotation based configuration is OK.
For example:

– regular expressions with matches

– access properties

SpEL supports nested properties with standard syntax: object.prop1.prop2.prop3

Another way to get properties is using EvaluationContext, we can also set property value:

To access resource properties myapp.properties:

We can use SpEL by this way:

Run the example, we will get the result:

– invoking constructors

The example will construct new String and new Customer object with constructor methods.

– array constructions

– inline Lists and Maps

– access List and Map objects

Assume that we need to get item from List and Map of an object:

By using annotation-based configuration:

By using xml-based configuration:

– method invocation

The example will invoke method substring() and toUpperCase() in expression.

– collection selection and projection

Selection is a powerful feature. We can get a new Collection by selecting entries from an old Collection.
Syntax: collection.?[selectionExpression]
Projection allows a collection to drive the evaluation of a sub-expression and the result is a new collection with evaluation content.
Syntax: collection.![projectionExpression]

The example gets a new collection (Map) in which all remainYearTimes are larger than 2, then evaluates with a List of Boolean value in which each item is a evaluated result of corresponding item in the old collection.

– assignment

In the example about access properties above, we assign a value to a property using setValue()

Now we can also assign with clearer expression String: age = 43 using getValue()

– types

T operator can be used to specify an instance of java.lang.Class.

We can also invoke Static methods of this class instance.

– bean references

SpEL can be used to reference to a Bean or field/method of that Bean.

service bean references to anotherCustomer bean.
anotherCustomer bean references to newCustomer bean method getAge() and field lastName.
Run the example, we will get the result similar this (depending on random value, we will get different results):

III. Source code
1. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)

2. Project Structure


3. Download Link


4. Check results

– Config maven build:
clean install
– Run project with mode Java Application
– Check results in Console Screen.

Related Posts

    Got Something To Say:

    Your email address will not be published. Required fields are marked *