Spring Expression Language (SpEL)¶
Spring Expression Language (SpEL) is a powerful expression language that supports querying and manipulating an object graph at runtime. It is used extensively within the Spring Framework for evaluating expressions in XML configuration files and annotations (such as @Value).^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md]
The syntax typically involves delimiters: #{ ... } for SpEL expressions and ${ ... } for property placeholders^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md].
Core Concepts¶
Parser and Context¶
SpEL relies on a few key classes to process expressions:
SpelExpressionParser: The parser used to parse raw expression strings into compiledExpressionobjects^[600-developer__spring__spring-spel.md].EvaluationContext: (SpecificallyStandardEvaluationContext) The context used to configure properties such as variables, functions, and type resolution during evaluation^[600-developer__spring-spring-spel.md].
The process involves calling parser.parseExpression(content) followed by exp.getValue(context)^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md].
Syntax and Operators¶
SpEL supports a variety of literal and operator expressions^[600-developer-spring-spring-spel.md]:
- Literals: Strings, numbers, and boolean values^[600-developer-spring-spring-spel.md].
- Relational Operators: Comparisons like
==,eq,lt,le,gt,ge^[600-developer-spring-spring-spel.md]. - Logical Operators:
and,or,not^[600-developer-spring-spring-spel.md]. - Mathematical Operators: Basic arithmetic like
+,-,*,/^[600-developer-spring-spring-spel.md]. - Ternary Operator: The standard
? :conditional logic^[600-developer-spring-spring-spel.md].
Integration with Spring¶
Bean Definitions¶
In XML configuration, SpEL can be used to inject values or reference other beans dynamically^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md].
For example, to inject a specific item from a list property of another bean:
<property name="topic" value="#{tutorial.topicsList[1]}"/>^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md]
Annotations¶
SpEL is commonly used with the @Value annotation to inject values into fields^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md].
@Value("#{tutorial.topicsList[0]}")
private Topic defaultTopic;
Advanced Features¶
Type References and Methods¶
SpEL allows for direct interaction with Java classes via the T() operator and method invocation^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md].
- Static Methods/Types:
T(java.lang.String)accesses a class type and can be used to call static methods^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md]. - Object Instantiation:
newcan be used to instantiate objects within an expression^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md]. - Method Calls: Methods on objects can be invoked directly^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md].
Variables and Collections¶
SpEL supports defining and manipulating collections and variables^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md].
- Inline Lists: Lists can be defined directly in expressions (e.g.,
{1, 2, 3})^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md]. - Inline Maps: Maps can be defined using key/value syntax (e.g.,
{key1: 'value1', key2: 'value2'})^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md]. - Variables: Variables can be defined on the
EvaluationContextand referenced in the expression^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md].
Related Concepts¶
- Spring Framework
- [[Dependency Injection]]
Sources¶
^[600-developer-spring-spring-spel.md, 600-developer__spring__spring-spel.md]