• April 26, 2016
  • Spring
  • Comments Off on Spring spel variable example

Spring spel variable example

Spring SpEL:

The SpEL stands for Spring Expression Language. It is a powerful expression language which supports querying and manipulating an object graph at the bean creation time or run time. It is similar to other expression languages like JSP EL, OGNL, MVEL and JBoss EL etc with some additional features like method invocation and basic string templating functionality.


The StandardEvaluationContext is a class which implements EvaluationContext interface. It uses a reflection mechanism to resolve properties or methods. We can set a variable using the method setVariable on the StandardEvaluationContext. We can use this variable in the expression by using the syntax #variableName.



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;


import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
 * Spring SPEL variable example.
 * @author javawithease
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.
	//Calculate result.



Download this example.

Comments are closed.