W3Schools Learner's Blog

w3Schools programming knowledge summary website

div

12/23/2017

Variable in SPEL | StandardEvaluationContext

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.   
  5. public class Test {  
  6. public static void main(String[] args) {  
  7. Calculation calculation=new Calculation();  
  8. StandardEvaluationContext context=new StandardEvaluationContext(calculation);  
  9.       
  10. ExpressionParser parser = new SpelExpressionParser();  
  11. parser.parseExpression("number").setValue(context,"5");  
  12.       
  13. System.out.println(calculation.cube());  
  14. }  
  15. }  

No comments:

Post a Comment

Note: only a member of this blog may post a comment.