SpEL parsing and evaluation process¶
The Spring Expression Language (SpEL) parsing and evaluation process involves a distinct workflow where raw expression strings are transformed into executable logic through specific parser and context classes.^[600-developer-spring-spring-spel.md]
Core Components¶
The process relies on three primary components:
SpelExpressionParser: The standard parser used to compile expression strings into an executable form.^[600-developer-spring-spring-spel.md]Expression: The object returned after parsing, encapsulating the compiled expression.^[600-developer-spring-spring-spel.md]EvaluationContext: The context configuration (often implemented asStandardEvaluationContext) used to set variables, root objects, and other execution parameters.^[600-developer-spring-spring-spel.md]
Parsing Phase¶
The first step is parsing, which transforms a raw string into a parsed expression object.^[600-developer-spring-spring-spel.md] This is typically initiated via the parseExpression method.^[600-developer-spring-spring-spel.md]
When parsing, a ParserContext can be supplied to define the expression delimiters. The default delimiter is the standard #{ ... } syntax.^[600-developer-spring-spring-spel.md] For example, ParserContext.TEMPLATE_EXPRESSION is used when parsing a string that acts as a template containing an embedded SpEL expression.^[600-developer-spring-spring-spel.md]
Evaluation Phase¶
Once parsed, the expression is executed using the getValue method.^[600-developer-spring-spring-spel.md] This step requires an EvaluationContext to resolve variables, properties, and method references defined within the expression string.^[600-developer-spring-spring-spel.md]
Reflection and Method Calls¶
During evaluation, SpEL utilizes Java Reflection to handle logic.^[600-developer-spring-spring-spel.md] This allows the expression to perform actions such as:
- Invoking static methods on classes (e.g., using
T()).^[600-developer-spring-spring-spel.md] - Instantiating new objects using the
newoperator.^[600-developer-spring-spring-spel.md] - Accessing properties and calling methods on the root object or other beans defined in the context.^[600-developer-spring-spring-spel.md]
Sources¶
^[600-developer-spring-spring-spel.md]
Related¶
- Spring Framework
- [[Reflection (computer science)]]