The expression language supports the following functionality
Literal expressions
Boolean and relational operators
Regular expressions
Class expressions
Accessing properties, arrays, lists, maps
Method invocation
Relational operators
Assignment
Calling constructors
Bean references
Array construction
Inline lists
Ternary operator
Variables
User defined functions
Collection projection
Collection selection
Templated expressions
This section introduces the simple use of SpEL interfaces and its
expression language. The complete language reference can be found in the
section
Language
Reference
.
The following code introduces the SpEL API to evaluate the literal
string expression 'Hello World'.
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'");
String message = (String) exp.getValue();
The value of the
message variable is simply '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.
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'.concat('!')");
String message = (String) exp.getValue();
The value of message is now 'Hello World!'.
As an example of calling a JavaBean property, the String property
'Bytes' can be called as shown below.
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'.bytes");
byte[] bytes = (byte[]) exp.getValue();
SpEL also supports nested properties using standard 'dot' notation,
i.e. prop1.prop2.prop3 and the setting of property values
Public fields may also be accessed.
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'.bytes.length");
int length = (Integer) exp.getValue();
The String's constructor can be called instead of using a string
literal.
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("new String('hello world').toUpperCase()");
String message = exp.getValue(String.class);
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.
GregorianCalendar c = new GregorianCalendar();
c.set(1856, 7, 9);
Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("name");
EvaluationContext context = new StandardEvaluationContext(tesla);
String name = (String) exp.getValue(context);
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:
/ Create and set a calendar
GregorianCalendar c = new GregorianCalendar();
c.set(1856, 7, 9);
Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("name");
String name = (String) exp.getValue(tesla);
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.
The StandardEvaluationContext is relatively expensive to construct and
during repeated usage it builds up cached state that enables subsequent
expression evaluations to be performed more quickly. For this reason it is
better to cache and reuse them where possible, rather than construct a new
one for each expression evaluation.
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.
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.
Expression exp = parser.parseExpression("name == 'Nikola Tesla'");
boolean result = exp.getValue(context, Boolean.class);
The interface
EvaluationContext
is
used when evaluating an expression to resolve properties, methods,
fields, and to help perform type conversion. The out-of-the-box
implementation,
StandardEvaluationContext
, uses
reflection to manipulate the object, caching
java.lang.reflect
's
Method
,
Field
, and
Constructor
instances for increased performance.
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
ConstructorResolver
s,
MethodResolver
s, and
PropertyAccessor
s to extend how SpEL evaluates
expressions. Please refer to the JavaDoc of these classes for more
details.
By default SpEL uses the conversion service available in Spring
(
org.springframework.core.convert.ConversionService
).
This conversion service comes with many converters built in for common
conversions but is also fully extensible so custom conversions between
types can be added. Additionally it has the key capability that it is
generics aware. This means that when working with generic types in
expressions, SpEL will attempt conversions to maintain type
correctness for any objects it encounters.
What does this mean in practice? Suppose assignment, using
setValue()
, is being used to set a
List
property. The type of the property is actually
List<Boolean>
. SpEL will recognize that the
elements of the list need to be converted to
Boolean
before being placed in it. A simple
example:
class Simple {
public List<Boolean> booleanList = new ArrayList<Boolean>();
Simple simple = new Simple();
simple.booleanList.add(true);
StandardEvaluationContext simpleContext = new StandardEvaluationContext(simple);
parser.parseExpression("booleanList[0]").setValue(simpleContext, "false");
Boolean b = simple.booleanList.get(0);
SpEL expressions can be used with XML or annotation based
configuration metadata for defining BeanDefinitions. In both cases the
syntax to define the expression is of the form #{ <expression
string> }
.
A property or constructor-arg value can be set using expressions
as shown below
<bean id="numberGuess" class="org.spring.samples.NumberGuess">
<property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>
</bean>
The variable 'systemProperties' is predefined, so you can use it
in your expressions as shown below. Note that you do not have to prefix
the predefined variable with the '#' symbol in this context.
<bean id="taxCalculator" class="org.spring.samples.TaxCalculator">
<property name="defaultLocale" value="#{ systemProperties['user.region'] }"/>
</bean>
You can also refer to other bean properties by name, for
example.
<bean id="numberGuess" class="org.spring.samples.NumberGuess">
<property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>
</bean>
<bean id="shapeGuess" class="org.spring.samples.ShapeGuess">
<property name="initialShapeSeed" value="#{ numberGuess.randomNumber }"/>
</bean>
The @Value
annotation can be placed on fields,
methods and method/constructor parameters to specify a default
value.
Here is an example to set the default value of a field
variable.
public static class FieldValueTestBean
@Value("#{ systemProperties['user.region'] }")
private String defaultLocale;
public void setDefaultLocale(String defaultLocale)
this.defaultLocale = defaultLocale;
public String getDefaultLocale()
return this.defaultLocale;
The equivalent but on a property setter method is shown
below.
public static class PropertyValueTestBean
private String defaultLocale;
@Value("#{ systemProperties['user.region'] }")
public void setDefaultLocale(String defaultLocale)
this.defaultLocale = defaultLocale;
public String getDefaultLocale()
return this.defaultLocale;
Autowired methods and constructors can also use the
@Value
annotation.
public class SimpleMovieLister {
private MovieFinder movieFinder;
private String defaultLocale;
@Autowired
public void configure(MovieFinder movieFinder,
@Value("#{ systemProperties['user.region'] }") String defaultLocale) {
this.movieFinder = movieFinder;
this.defaultLocale = defaultLocale;
public class MovieRecommender {
private String defaultLocale;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao,
@Value("#{systemProperties['user.country']}") String defaultLocale) {
this.customerPreferenceDao = customerPreferenceDao;
this.defaultLocale = defaultLocale;
The types of literal expressions supported are strings, numeric values
(int, real, hex), boolean and null. Strings are delimited by single quotes.
To put a single quote itself in a string, use two single quote characters.
The following listing shows simple usage of literals. Typically they would not
be used in isolation like this but rather as part of a more complex expression,
for example using a literal on one side of a logical comparison operator.
ExpressionParser parser = new SpelExpressionParser();
String helloWorld = (String) parser.parseExpression("'Hello World'").getValue();
double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();
boolean trueValue = (Boolean) parser.parseExpression("true").getValue();
Object nullValue = parser.parseExpression("null").getValue();
Numbers support the use of the negative sign, exponential
notation, and decimal points. By default real numbers are parsed using
Double.parseDouble().
Navigating with property references is easy, just use a period to
indicate a nested property value. The instances of Inventor class, pupin
and tesla, were populated with data listed in the section Classes used in the
examples. To navigate "down" and get Tesla's year of birth and
Pupin's city of birth the following expressions are used.
int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context);
String city = (String) parser.parseExpression("placeOfBirth.City").getValue(context);
Case insensitivity is allowed for the first letter of property
names. The contents of arrays and lists are obtained using square
bracket notation.
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext teslaContext = new StandardEvaluationContext(tesla);
String invention = parser.parseExpression("inventions[3]").getValue(teslaContext,
String.class);
StandardEvaluationContext societyContext = new StandardEvaluationContext(ieee);
String name = parser.parseExpression("Members[0].Name").getValue(societyContext, String.class);
String invention = parser.parseExpression("Members[0].Inventions[6]").getValue(societyContext,
String.class);
The contents of maps are obtained by specifying the literal key
value within the brackets. In this case, because keys for the Officers
map are strings, we can specify string literals.
Inventor pupin = parser.parseExpression("Officers['president']").getValue(societyContext,
Inventor.class);
String city =
parser.parseExpression("Officers['president'].PlaceOfBirth.City").getValue(societyContext,
String.class);
parser.parseExpression("Officers['advisors'][0].PlaceOfBirth.Country").setValue(societyContext,
"Croatia");
Lists can be expressed directly in an expression using {} notation.
List numbers = (List) parser.parseExpression("{1,2,3,4}").getValue(context);
List listOfLists = (List) parser.parseExpression("{{'a','b'},{'x','y'}}").getValue(context);
{} by itself means an empty list. For performance reasons, if the
list is itself entirely composed of fixed literals then a constant list is created
to represent the expression, rather than building a new list on each evaluation.
Arrays can be built using the familiar Java syntax, optionally
supplying an initializer to have the array populated at construction time.
int[] numbers1 = (int[]) parser.parseExpression("new int[4]").getValue(context);
int[] numbers2 = (int[]) parser.parseExpression("new int[]{1,2,3}").getValue(context);
int[][] numbers3 = (int[][]) parser.parseExpression("new int[4][5]").getValue(context);
It is not currently allowed to supply an initializer when constructing
a multi-dimensional array.
Methods are invoked using typical Java programming syntax. You may
also invoke methods on literals. Varargs are also supported.
String c = parser.parseExpression("'abc'.substring(2, 3)").getValue(String.class);
boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')").getValue(societyContext, Boolean.class);
The relational operators; equal, not equal, less than, less than
or equal, greater than, and greater than or equal are supported using
standard operator notation.
boolean trueValue = parser.parseExpression("2 == 2").getValue(Boolean.class);
boolean falseValue = parser.parseExpression("2 < -5.0").getValue(Boolean.class);
boolean trueValue = parser.parseExpression("'black' < 'block'").getValue(Boolean.class);
Greater/less-than comparisons against `null` follow a simple rule: `null`
is treated as nothing here (i.e. NOT as zero). As a consequence, any other value
is always greater than `null` (`X > null` is always `true`) and no other value
is ever less than nothing (`X < null` is always `false`). If you prefer numeric
comparisons instead, please avoid number-based `null` comparisons in favor of
comparisons against zero (e.g. `X > 0` or `X < 0`).
In addition to standard relational operators SpEL supports the
'instanceof' and regular expression based 'matches' operator.
boolean falseValue = parser.parseExpression("'xyz' instanceof T(int)").getValue(Boolean.class);
boolean trueValue =
parser.parseExpression("'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);
boolean falseValue =
parser.parseExpression("'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);
Each symbolic operator can also be specified as a purely alphabetic equivalent.
This avoids problems where the symbols used have special meaning for the document type
in which the expression is embedded (eg. an XML document). The textual equivalents are
shown here: lt ('<'), gt ('>'), le ('<='), ge ('>='), eq ('=='), ne ('!='),
div ('/'), mod ('%'), not ('!'). These are case insensitive.
The logical operators that are supported are and, or, and not.
Their use is demonstrated below.
boolean falseValue = parser.parseExpression("true and false").getValue(Boolean.class);
String expression = "isMember('Nikola Tesla') and isMember('Mihajlo Pupin')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);
boolean trueValue = parser.parseExpression("true or false").getValue(Boolean.class);
String expression = "isMember('Nikola Tesla') or isMember('Albert Einstein')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);
boolean falseValue = parser.parseExpression("!true").getValue(Boolean.class);
String expression = "isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')";
boolean falseValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);
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. Other mathematical operators
supported are modulus (%) and exponential power (^). Standard operator
precedence is enforced. These operators are demonstrated below.
int two = parser.parseExpression("1 + 1").getValue(Integer.class);
String testString =
parser.parseExpression("'test' + ' ' + 'string'").getValue(String.class);
int four = parser.parseExpression("1 - -3").getValue(Integer.class);
double d = parser.parseExpression("1000.00 - 1e4").getValue(Double.class);
int six = parser.parseExpression("-2 * -3").getValue(Integer.class);
double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(Double.class);
int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class);
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(Double.class);
int three = parser.parseExpression("7 % 4").getValue(Integer.class);
int one = parser.parseExpression("8 / 5 % 2").getValue(Integer.class);
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class);
Setting of a property is done by using the assignment operator.
This would typically be done within a call to
setValue
but can also be done inside a call to
getValue
.
Inventor inventor = new Inventor();
StandardEvaluationContext inventorContext = new StandardEvaluationContext(inventor);
parser.parseExpression("Name").setValue(inventorContext, "Alexander Seovic2");
String aleks = parser.parseExpression("Name = 'Alexandar Seovic'").getValue(inventorContext,
String.class);
The special 'T' operator can be used to specify an instance of
java.lang.Class (the 'type'). Static methods are invoked using this
operator as well. The StandardEvaluationContext
uses a TypeLocator
to find types and the
StandardTypeLocator
(which can be replaced) is
built with an understanding of the java.lang package. This means T()
references to types within java.lang do not need to be fully qualified,
but all other type references must be.
Class dateClass = parser.parseExpression("T(java.util.Date)").getValue(Class.class);
Class stringClass = parser.parseExpression("T(String)").getValue(Class.class);
boolean trueValue =
parser.parseExpression("T(java.math.RoundingMode).CEILING < T(java.math.RoundingMode).FLOOR")
.getValue(Boolean.class);
Constructors can be invoked using the new operator. The fully
qualified class name should be used for all but the primitive type and
String (where int, float, etc, can be used).
Inventor einstein =
p.parseExpression("new org.spring.samples.spel.inventor.Inventor('Albert Einstein',
'German')")
.getValue(Inventor.class);
p.parseExpression("Members.add(new org.spring.samples.spel.inventor.Inventor('Albert Einstein',
'German'))")
.getValue(societyContext);
Variables can be referenced in the expression using the syntax
#variableName. Variables are set using the method setVariable on the
StandardEvaluationContext.
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
StandardEvaluationContext context = new StandardEvaluationContext(tesla);
context.setVariable("newName", "Mike Tesla");
parser.parseExpression("Name = #newName").getValue(context);
System.out.println(tesla.getName())
The variable #this is always defined and refers to the current
evaluation object (against which unqualified references are resolved).
The variable #root is always defined and refers to the root
context object. Although #this may vary as components of an expression
are evaluated, #root always refers to the root.
List<Integer> primes = new ArrayList<Integer>();
primes.addAll(Arrays.asList(2,3,5,7,11,13,17));
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("primes",primes);
List<Integer> primesGreaterThanTen =
(List<Integer>) parser.parseExpression("#primes.?[#this>10]").getValue(context);
You can extend SpEL by registering user defined functions that can
be called within the expression string. The function is registered with
the StandardEvaluationContext
using the
method.
public void registerFunction(String name, Method m)
A reference to a Java Method provides the implementation of the
function. For example, a utility method to reverse a string is shown
below.
public abstract class StringUtils {
public static String reverseString(String input) {
StringBuilder backwards = new StringBuilder();
for (int i = 0; i < input.length(); i++)
backwards.append(input.charAt(input.length() - 1 - i));
return backwards.toString();
This method is then registered with the evaluation context and can
be used within an expression string.
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.registerFunction("reverseString",
StringUtils.class.getDeclaredMethod("reverseString",
new Class[] { String.class }));
String helloWorldReversed =
parser.parseExpression("#reverseString('hello')").getValue(context, String.class);
If the evaluation context has been configured with a bean resolver it is possible to
lookup beans from an expression using the (@) symbol.
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new MyBeanResolver());
Object bean = parser.parseExpression("@foo").getValue(context);
You can use the ternary operator for performing if-then-else
conditional logic inside the expression. A minimal example is:
String falseString =
parser.parseExpression("false ? 'trueExp' : 'falseExp'").getValue(String.class);
In this case, the boolean false results in returning the string
value 'falseExp'. A more realistic example is shown below.
parser.parseExpression("Name").setValue(societyContext, "IEEE");
societyContext.setVariable("queryName", "Nikola Tesla");
expression = "isMember(#queryName)? #queryName + ' is a member of the ' " +
"+ Name + ' Society' : #queryName + ' is not a member of the ' + Name + ' Society'";
String queryResultString =
parser.parseExpression(expression).getValue(societyContext, String.class);
Also see the next section on the Elvis operator for an even
shorter syntax for the ternary operator.
The Elvis operator is a shortening of the ternary operator syntax
and is used in the Groovy
language. With the ternary operator syntax you usually have to repeat a
variable twice, for example:
String name = "Elvis Presley";
String displayName = name != null ? name : "Unknown";
Instead you can use the Elvis operator, named for the resemblance
to Elvis' hair style.
ExpressionParser parser = new SpelExpressionParser();
String name = parser.parseExpression("null?:'Unknown'").getValue(String.class);
System.out.println(name);
Here is a more complex example.
ExpressionParser parser = new SpelExpressionParser();
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
StandardEvaluationContext context = new StandardEvaluationContext(tesla);
String name = parser.parseExpression("Name?:'Elvis Presley'").getValue(context, String.class);
System.out.println(name);
tesla.setName(null);
name = parser.parseExpression("Name?:'Elvis Presley'").getValue(context, String.class);
System.out.println(name);
The Safe Navigation operator is used to avoid a
NullPointerException
and comes from the Groovy
language. Typically when you have a reference to an object you might
need to verify that it is not null before accessing methods or
properties of the object. To avoid this, the safe navigation operator
will simply return null instead of throwing an exception.
ExpressionParser parser = new SpelExpressionParser();
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
tesla.setPlaceOfBirth(new PlaceOfBirth("Smiljan"));
StandardEvaluationContext context = new StandardEvaluationContext(tesla);
String city = parser.parseExpression("PlaceOfBirth?.City").getValue(context, String.class);
System.out.println(city);
tesla.setPlaceOfBirth(null);
city = parser.parseExpression("PlaceOfBirth?.City").getValue(context, String.class);
System.out.println(city);
The Elvis operator can be used to apply default values in
expressions, e.g. in an @Value
expression:
@Value("#{systemProperties['pop3.port'] ?: 25}")
This will inject a system property pop3.port
if it
is defined or 25 if not.