SpEL Variable and Method Operations¶
In the Spring Expression Language (SpEL), variables and methods are fundamental constructs used to interact with the EvaluationContext and bean definitions. Operations range from defining root variables to invoking static methods, instance methods, and constructors.
Variable Operations¶
Root Variables¶
The context of a SpEL expression can be anchored by a "root" object. This allows subsequent expressions to resolve properties or methods relative to that root without re-specifying the object.^[600-developer__spring__spring-spel.md]
In standard Spring configurations, this is often implicit. For example, in XML configuration, a bean defined via <property name="topic" value="#{tutorial.topicsList[1]}"/> implies tutorial acts as a root variable for the lookup.^[600-developer__spring__spring-spel.md]
Passing Parameters¶
Variables can be explicitly passed into an evaluation context. This involves defining the variable within the StandardEvaluationContext and referencing it within the expression string (e.g., #variableName).^[600-developer__spring__spring-spel.md]
Method Operations¶
Static Methods¶
SpEL allows the invocation of static methods on Java classes using the T() operator. The syntax requires the fully qualified class name followed by the method invocation.^[600-developer__spring__spring-spel.md]
For example, calling a static method from the String class would look like T(java.lang.String).format(...).^[600-developer__spring__spring-spel.md]
Constructors¶
New object instances can be created directly within expressions using the new operator. This is typically combined with the T() operator to reference the class type.^[600-developer__spring__spring-spel.md]
Instance Methods¶
Methods belonging to a specific bean instance can be invoked using standard dot notation.^[600-developer__spring__spring-spel.md]
For instance, given a bean tutorial, a method getTopics() can be accessed via tutorial.getTopics().^[600-developer__spring__spring-spel.md]
Implementation Details¶
Functionally, these operations rely on the EvaluationContext to resolve references. While the context configuration (e.g., StandardEvaluationContext) handles the reflection and binding of variables and methods, the expression string drives the logic.^[600-developer__spring__spring-spel.md]
Related Concepts¶
- Spring Framework
- [[SpEL Literal and Mathematical Expressions]]
- [[SpEL Collection Operations]]
Sources¶
600-developer__spring__spring-spel.md