Open In App

Spring – Variable in SpEL

Improve
Improve
Like Article
Like
Save
Share
Report

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

  1. Literal expressions
  2. Boolean and relational operators
  3. Regular expressions
  4. Class expressions
  5. Accessing properties, arrays, lists, maps
  6. Method invocation
  7. Relational operators
  8. Assignment
  9. Calling constructors
  10. Bean references
  11. Array construction
  12. Inline lists
  13. Inline maps
  14. Ternary operator
  15. Variables
  16. User-defined functions
  17. Collection projection
  18. Collection selection
  19. Templated expressions

Syntax:

context.setVariable("newName", "Geeksforgeeks");

Implementation:

We will be demonstrating via the help of two listed files as follows:

  1. Multiplication.java − Multiplication class.
  2. 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



Last Updated : 25 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads