Spring Expression Language Operators and Variables (SpEL)

1. Objective

In the previous article of you have seen Spring Expression Language. Here today we will see the Spring Expression Language Operators and Variables. Along with that, we will have working examples of Spring SpEL Operators and Variable.
So, let’s start Spring Expression Language Operators and Variables.

Spring Expression Language Operators and Variables (SpEL)

Spring Expression Language Operators and Variables (SpEL)

2. Spring Expression Language Variables and Operators

There are several operators which are there in Spring SpEL such as arithmetic, logical etc. Below is the example where operators are used. For that create a Java class in Eclipse IDE called Test. In that use the arithmetic, logical, Relational operators. After that run the program to check the output.
Do you know about Spring Transaction Management
Test.java:

import org.springframework.expression.ExpressionParser;  
import org.springframework.expression.spel.standard.SpelExpressionParser;  
public class Test {  
public static void main(String[] args) {  
ExpressionParser parser = new SpelExpressionParser();  
//arithmetic operator  
System.out.println(parser.parseExpression("'Welcome SPEL'+'!'").getValue());  
System.out.println(parser.parseExpression("10 * 10/2").getValue());  
System.out.println(parser.parseExpression("'Today is: '+ new java.util.Date()").getValue());  
//logical operator  
System.out.println(parser.parseExpression("true and true").getValue());  
//Relational operator  
System.out.println(parser.parseExpression("'sonoo'.length()==5").getValue());  
}  
}  

Now after seeing the Spring Expression Language operators, you will see what are the variables in Spring Expression Language and how to use them. In SpEL you have to store the values in the variables and use the variable in the method and call that method. To work on the SpEL variable you have to use Standard EvaluationContext class. This class implements the EvaluationContext interface. It has a reflection mechanism which helps to resolve properties and methods. You have to set the variables calling the method called setVariable on the StandardEvaluationContext. Also, you can use this variable in the expression by using the syntax #variableName.
Below you will be seeing the example of variables in Spring Expression Language.
Read about Spring BeanPostProcessors 
MutiplicationTest.java:

public class MulitplicationTest {
   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;
  }
}

MultiplicationTest.java:

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
/**
* Spring SPEL variable example.
* @author example
*/
public class Test {
public static void main(String args[]){
//Create MulitplicationTest object.
MulitplicationTest mulitplicationTest=new MulitplicationTest();
//Create StandardEvaluationContext object
//with MulitplicationTest object.
StandardEvaluationContext context=
new StandardEvaluationContext(mulitplicationTest);
//Create a parser with default settings.
ExpressionParser parser = new SpelExpressionParser();
//Set variables values.
parser.parseExpression("num1").setValue(context,"10");
parser.parseExpression("num2").setValue(context,"20");
//Calculate result.
System.out.println(mulitplicationTest.multiplication());
}
}

Output – 200
Do you know about Spring AOP 
So, this was all about Spring Expression Language Operators and Variables. Hope you like our explanation.

3. Conclusion

Hence, in this article, we got to know more about the Spring Expression Language which is a powerful expression language that supports querying and manipulating an object graph at bean creation or runtime. we studied the Spring Expression Language Operators and Variables (SpEL) and how to declare and use them in your application.
Related Topic- Spring MVC Tiles 3 Integration 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.