Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Variable in SPEL


In SpEL, we can store a value in the variable and use the variable in the method and call the method. To work on variable, we need to use StandardEvaluationContext class.


Example of Using variable in SPEL

Calculation.java

  1. public class Calculation {
  2. private int number;
  3. public int getNumber() {
  4.     return number;
  5. }
  6. public void setNumber(int number) {
  7.     this.number = number;
  8. }
  9. public int cube(){
  10.     return number*number*number;
  11. }
  12. }

Test.java

  1. import org.springframework.expression.ExpressionParser;
  2. import org.springframework.expression.spel.standard.SpelExpressionParser;
  3. import org.springframework.expression.spel.support.StandardEvaluationContext;
  4. public class Test {
  5. public static void main(String[] args) {
  6. Calculation calculation=new Calculation();
  7. StandardEvaluationContext context=new StandardEvaluationContext(calculation);
  8. ExpressionParser parser = new SpelExpressionParser();
  9. parser.parseExpression(“number”).setValue(context,“5”);
  10. System.out.println(calculation.cube());
  11. }
  12. }

Comments

comments