tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > Expression Language > Evaluation Context

Evaluation Context 

The Spring Expression Language (SpEL) is a simple and powerful expression language which helps to query and manipulate objects at runtime. The following example shows using evaluation context. It provides a namespace for accessing variables.

File Name  :  
com/bethecoder/tutorials/spring3/tests/spel/EvaluationContextTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.spring3.tests.spel;

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

import com.bethecoder.tutorials.spring3.basic.Employee;

public class EvaluationContextTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    Employee emp = new Employee(1"BTC");
    StandardEvaluationContext stdContext = new StandardEvaluationContext(emp);
    
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression("name");
    String value = exp.getValue(stdContext, String.class)//emp.getName()
    System.out.println(value);

    exp = parser.parseExpression("name == 'BTC'");
    boolean result = exp.getValue(stdContext, Boolean.class)
    System.out.println(result);
    
    
    Employee emp2 = new Employee(2"ABC");
    exp = parser.parseExpression("name");  //emp2.getName()
    value = exp.getValue(emp2, String.class)//creates default evaluation context internally
    System.out.println(value);
  }

}
   

It gives the following output,
BTC
true
ABC



 
  


  
bl  br