EvaluationContext interface is implemented by the StandardEvaluationContext class. To resolve properties or methods, it employs a reflection technique. The method setVariable on the StandardEvaluationContext can be used to set a variable. Using the notation #variableName, we may utilize this variable in the expression.
We can put a value in the variable and then call the method using the variable. We must utilize the StandardEvaluationContext class to interact with variables.
While there are various alternative Java expression languages available, such as OGNL, MVEL, and JBoss EL, the Spring Expression Language was established to offer the Spring community a single well-supported expression language that can be utilized throughout the Spring portfolio of products. The language’s features are dictated by the needs of the Spring portfolio’s projects, including tooling requirements for code completion assistance inside the Eclipse-based Spring Tool Suite. That stated, SpEL is based on a technology-neutral API allowing alternative expression language implementations to be added should the need arise.
SpEL is a basis for expression evaluation inside the Spring portfolio, however, it is not linked to Spring and may be used independently. Many of the examples in this chapter employ SpEL as if it were a separate expression language in order to be self-contained. This necessitates the creation of a few infrastructure classes, such as the parser. Most Spring users will avoid dealing with this infrastructure and instead focus on writing expression strings for evaluation. The integration of SpEL into the creation of XML or annotation-based bean definitions, as illustrated in the section Expression support for defining bean definitions, is an example of this common use.
Features of SpEL
- Literal expressions
- Boolean and relational operators
- Regular expressions
- Class expressions
- Accessing properties, arrays, lists, maps
- Method invocation
- Relational operators
- Assignment
- Calling constructors
- Bean references
- Array construction
- Inline lists
- Inline maps
- Ternary operator
- Variables
- User-defined functions
- Collection projection
- Collection selection
- Templated expressions
Syntax:
context.setVariable("newName", "Geeksforgeeks");
Implementation:
We will be demonstrating via the help of two listed files as follows:
- Multiplication.java − Multiplication class.
- Test.java − Main application to run and test.
A. File: Multiplication.java
Java
public class MultiplicationTest {
int num1;
int num2;
public int getNum1() { return num1; }
public void setNum1( int num1)
{
this .num1 = num1;
}
public int getNum2() { return num2; }
public void setNum2( int num2) { this .num2 = num2; }
public int multiplication() { return num1 * num2; }
}
|
B. File: Test.java
Java
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class Test {
public static void main(String args[])
{
MultiplicationTest multiplicationTest
= new MultiplicationTest();
StandardEvaluationContext context
= new StandardEvaluationContext(
multiplicationTest);
ExpressionParser parser
= new SpelExpressionParser();
parser.parseExpression( "num1" ).setValue(context,
"30" );
parser.parseExpression( "num2" ).setValue(context,
"30" );
System.out.println(
multiplicationTest.multiplication());
}
}
|
Output:
600

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 Mar, 2022
Like Article
Save Article