Open In App
Related Articles

Spring – Variable in SpEL

Improve Article
Save Article
Like Article

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


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


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

  1. − Multiplication class.
  2. − Main application to run and test.

A. File:


// 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:


// Java Program to Illustrate Spring SPEL Variable Example
// Importing required classes
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
// 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(
        // Creating  a parser with default settings.
        ExpressionParser parser
            = new SpelExpressionParser();
        // Setting variables values
        // Calculate result



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
Similar Reads
Complete Tutorials