Spring Expression Language (SpEL)
Spring Expression Language (SpEL)
6.1 Introduction
The Spring Expression Language (SpEL for short) is a powerful expression language that supports
querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL
but offers additional features, most notably method invocation and basic string templating
functionality.
While there are several other Java expression languages available, OGNL, MVEL, and JBoss EL,
to name a few, the Spring Expression Language was created to provide the Spring community with
a single well supported expression language that can be used across all the products in the Spring
portfolio. Its language features are driven by the requirements of the projects in the Spring
portfolio, including tooling requirements for code completion support within the eclipse based
SpringSource Tool Suite. That said, SpEL is based on a technology agnostic API allowing other
expression language implementations to be integrated should the need arise.
While SpEL serves as the foundation for expression evaluation within the Spring portfolio, it is not
directly tied to Spring and can be used independently. In order to be self contained, many of the
examples in this chapter use SpEL as if it were an independent expression language. This requires
creating a few bootstrapping infrastructure classes such as the parser. Most Spring users will not
need to deal with this infrastructure and will instead only author expression strings for evaluation.
An example of this typical use is the integration of SpEL into creating XML or annotated based
bean definitions as shown in the section Expression support for defining bean definitions.
This chapter covers the features of the expression language, its API, and its language syntax. In
several places an Inventor and Inventor's Society class are used as the target objects for
expression evaluation. These class declarations and the data used to populate them are listed at
the end of the chapter.
6.2 Feature Overview
The expression language supports the following functionality
Literal expressions
Regular expressions
Class expressions
Method invocation
Relational operators
Assignment
Calling constructors
Bean references
Array construction
Inline lists
https://docs.spring.io/spring-framework/docs/3.0.x/reference/expressions.html 1/4
7/31/22, 1:40 PM 6. Spring Expression Language (SpEL)
Ternary operator
Variables
Collection projection
Collection selection
Templated expressions
The following code introduces the SpEL API to evaluate the literal string expression 'Hello World'.
The SpEL classes and interfaces you are most likely to use are located in the
packages org.springframework.expression and its sub packages and spel.support.
The interface ExpressionParser is responsible for parsing an expression string. In this example the
expression string is a string literal denoted by the surrounding single quotes. The
interface Expression is responsible for evaluating the previously defined expression string. There
are two exceptions that can be thrown, ParseException and EvaluationException when calling
'parser.parseExpression' and 'exp.getValue' respectively.
SpEL supports a wide range of features, such as calling methods, accessing properties, and
calling constructors.
As an example of method invocation, we call the 'concat' method on the string literal.
As an example of calling a JavaBean property, the String property 'Bytes' can be called as shown
below.
// invokes 'getBytes()'
SpEL also supports nested properties using standard 'dot' notation, i.e. prop1.prop2.prop3 and the
setting of property values
https://docs.spring.io/spring-framework/docs/3.0.x/reference/expressions.html 2/4
7/31/22, 1:40 PM 6. Spring Expression Language (SpEL)
// invokes 'getBytes().length'
Note the use of the generic method public <T> T getValue(Class<T> desiredResultType). Using this
method removes the need to cast the value of the expression to the desired result type.
An EvaluationException will be thrown if the value cannot be cast to the type T or converted using
the registered type converter.
The more common usage of SpEL is to provide an expression string that is evaluated against a
specific object instance (called the root object). There are two options here and which to choose
depends on whether the object against which the expression is being evaluated will be changing
with each call to evaluate the expression. In the following example we retrieve the name property
from an instance of the Inventor class.
c.set(1856, 7, 9);
In the last line, the value of the string variable 'name' will be set to "Nikola Tesla". The class
StandardEvaluationContext is where you can specify which object the "name" property will be
evaluated against. This is the mechanism to use if the root object is unlikely to change, it can
simply be set once in the evaluation context. If the root object is likely to change repeatedly, it can
be supplied on each call to getValue, as this next example shows:
c.set(1856, 7, 9);
In this case the inventor tesla has been supplied directly to getValue and the expression evaluation
infrastructure creates and manages a default evaluation context internally - it did not require one to
be supplied.
https://docs.spring.io/spring-framework/docs/3.0.x/reference/expressions.html 3/4
7/31/22, 1:40 PM 6. Spring Expression Language (SpEL)
In some cases it can be desirable to use a configured evaluation context and yet still supply a
different root object on each call to getValue. getValue allows both to be specified on the same call.
In these situations the root object passed on the call is considered to override any (which maybe
null) specified on the evaluation context.
Note
In standalone usage of SpEL there is a need to create the parser, parse
expressions and perhaps provide evaluation contexts and a root context object.
However, more common usage is to provide only the SpEL expression string as
part of a configuration file, for example for Spring bean or Spring Web Flow
definitions. In this case, the parser, evaluation context, root object and any
predefined variables are all set up implicitly, requiring the user to specify
nothing other than the expressions.
As a final introductory example, the use of a boolean operator is shown using the Inventor object in
the previous example.
The StandardEvaluationContext is where you may specify the root object to evaluate against via the
method setRootObject() or passing the root object into the constructor. You can also specify
variables and functions that will be used in the expression using the
methods setVariable() and registerFunction(). The use of variables and functions are described in
the language reference sections Variables and Functions. The StandardEvaluationContext is also
where you can register custom ConstructorResolvers, MethodResolvers, and PropertyAccessors to
extend how SpEL evaluates expressions. Please refer to the JavaDoc of these classes for more
details.
https://docs.spring.io/spring-framework/docs/3.0.x/reference/expressions.html 4/4