SpEL variable operations and contexts¶
The SpEL (Spring Expression Language) variable operations and contexts allow for dynamic expression evaluation, method invocation, and variable management within the Spring Framework.^[600-developer-spring-spring-spel.md]
Core Components¶
SpEL evaluation relies on several key classes and interfaces:
- ExpressionParser: The
SpelExpressionParseris responsible for parsing expression strings into executableExpressionobjects.^[600-developer-spring-spring-spel.md] - EvaluationContext: The
StandardEvaluationContextis used to configure the evaluation environment, such as setting variables or defining the root object.^[600-developer-spring-spring-spel.md] - ParserContext: This interface (often implemented as
ParserContext.TEMPLATE_EXPRESSION) is used to define expression delimiters, typically enabling the#{ ... }syntax within a template string.^[600-developer-spring-spring-spel.md]
Variable Operations¶
Root Variables¶
A root object serves as the entry point for property access and method invocation within an expression.^[600-developer-spring-spring-spel.md] For example, in the expression #{tutorial.topicsList[0]}, tutorial acts as the root variable referencing a bean.^[600-developer-spring-spring-spel.md]
Setting Variables¶
Variables can be injected into the evaluation context programmatically. This allows expressions to reference defined placeholders using the syntax #variableName.^[600-developer-spring-spring-spel.md]
Method Invocation¶
SpEL supports calling methods on objects within the expression string.^[600-developer-spring-spring-spel.md]
Collection Operations¶
SpEL provides syntax for defining and manipulating inline collections:
- Lists: Lists can be defined directly within expressions.^[600-developer-spring-spring-spel.md]
- Maps: Maps can be defined and manipulated, including converting map entries to lists.^[600-developer-spring-spring-spel.md]
Contexts and Usage¶
Standard Evaluation Context¶
The StandardEvaluationContext is the standard implementation used to perform variable definitions and root object settings.^[600-developer-spring-spring-spel.md]
Annotation Integration¶
SpEL expressions are frequently used within Spring annotations, such as @Value, to inject values dynamically.^[600-developer-spring-spring-spel.md]
Reflection¶
SpEL utilizes Java reflection to handle class loading and method execution, such as accessing static methods via T() or instantiating new objects.^[600-developer-spring-spring-spel.md]
Sources¶
600-developer-spring-spring-spel.md