Spring – Variable in SpEL
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", "w3wiki");
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
// Java Program to Illustrate MultiplicationTest Class // Class public class MultiplicationTest { // Class data members int num1; int num2; // Getter public int getNum1() { return num1; } // Setter public void setNum1( int num1) { // this keyword refers to current // instance itself this .num1 = num1; } // Getter public int getNum2() { return num2; } // Setter public void setNum2( int num2) { this .num2 = num2; } // Method // To multiply two numbers public int multiplication() { return num1 * num2; } } |
B. File: Test.java
Java
// Java Program to Illustrate Spring SPEL Variable Example // Importing required classes import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; // Class public class Test { // Main driver method public static void main(String args[]) { // Creating MultiplicationTest object. MultiplicationTest multiplicationTest = new MultiplicationTest(); // Creating StandardEvaluationContext object // with MultiplicationTest object. StandardEvaluationContext context = new StandardEvaluationContext( multiplicationTest); // Creating a parser with default settings. ExpressionParser parser = new SpelExpressionParser(); // Setting variables values parser.parseExpression( "num1" ).setValue(context, "30" ); parser.parseExpression( "num2" ).setValue(context, "30" ); // Calculate result System.out.println( multiplicationTest.multiplication()); } } |
Output:
600
Contact Us