Variable in SPEL | StandardEvaluationContext

Variables can be referenced in the expression using the syntax #variableName. Variables are set using the method setVariable on the StandardEvaluationContext.

Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
StandardEvaluationContext context = new StandardEvaluationContext(tesla);
context.setVariable("newName", "Mike Tesla");

parser.parseExpression("Name = #newName").getValue(context);

System.out.println(tesla.getName()) // "Mike Tesla"

Example of Using variable in SPEL

Calculation.java
public class Calculation {
private int number;
public int getNumber() {
	return number;
}
public void setNumber(int number) {
	this.number = number;
}
public int cube(){
	return number*number*number;
}
}

Test.java
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class Test {
public static void main(String[] args) {
Calculation calculation=new Calculation();
StandardEvaluationContext context=new StandardEvaluationContext(calculation);
	
ExpressionParser parser = new SpelExpressionParser();
parser.parseExpression("number").setValue(context,"5");
	
System.out.println(calculation.cube());
}
}