SpEL expression types¶
Spring Expression Language (SpEL) supports a variety of expression types, ranging from simple literals to complex object manipulations.^[600-developer-spring-spring-spel.md]
Literal Expressions¶
SpEL supports standard literals for strings, dates, numbers (integers, real, and hex), as well as boolean and null values.^[600-developer-spring-spring-spel.md]
Mathematical, Relational, and Logical Expressions¶
The language supports symbolic operators for arithmetic, comparison, and logical operations (such as +, -, >, <, ==, and, not).^[600-developer-spring-spring-spel.md] It also supports the ternary operator for conditional logic, often used for concise if-then-else constructs (e.g., false ? 'trueExp' : 'falseExp').^[600-developer-spring-spring-spel.md]
Collection Expressions¶
SpEL provides syntax for defining and manipulating inline collections.^[600-developer-spring-spring-spel.md]
- List: Lists can be defined using curly brace notation (e.g.,
{1, 2, 3, 4}).^[600-developer-spring-spring-spel.md] - Map: Maps can be defined using key:value syntax within curly braces (e.g.,
{name:'Nikola', dob:'10-July-1856'}).^[600-developer-spring-spring-spel.md] Existing map contents can be modified or queried directly within the expression.^[600-developer-spring-spring-spel.md]
Class and Method Expressions¶
SpEL can interact with Java types and invoke methods.
- Class Type Expression: The
T()operator is used to specify a Java class, typically for accessing static methods or constants (e.g.,T(java.lang.String)).^[600-developer-spring-spring-spel.md] - Method Invocation: Methods on objects can be called using standard dot notation.^[600-developer-spring-spring-spel.md]
- Constructor Invocation: New objects can be instantiated using the
newoperator.^[600-developer-spring-spring-spel.md]
Variable and Property References¶
SpEL can reference variables defined in the evaluation context, including the root object.^[600-developer-spring-spring-spel.md]
- Variables: Custom variables can be defined and referenced within the expression using the syntax
#variableName.^[600-developer-spring-spring-spel.md] - Bean References: In a Spring context, beans can be referenced by their ID (e.g.,
@beanName).^[600-developer-spring-spring-spel.md] - Properties: Nested properties can be accessed safely using the null-safe navigation operator
?..^[600-developer-spring-spring-spel.md]
Related Concepts¶
- [[SpEL basics]]
- [[SpEL evaluation context]]
Sources¶
600-developer-spring-spring-spel.md