Variable Map
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 defining a variable map using SpEL.
package com.bethecoder.tutorials.spring3.tests.spel;
import java.util.HashMap;
import java.util.Map;
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 VariableMapTest {
/**
* @param args
*/
public static void main ( String [] args ) {
StandardEvaluationContext stdContext = new StandardEvaluationContext () ;
//Define a variable map
Map<String, Object> variables = new HashMap<String, Object> () ;
variables.put ( "org" , "BTC" ) ;
variables.put ( "emp" , new Employee ( 1 , "ABC" , 9999 )) ;
stdContext.setVariables ( variables ) ;
ExpressionParser parser = new SpelExpressionParser () ;
String value = parser.parseExpression ( "#org" ) .getValue ( stdContext, String. class ) ;
System.out.println ( value ) ;
value = parser.parseExpression ( "#emp.salary" ) .getValue ( stdContext, String. class ) ;
System.out.println ( value ) ;
boolean resp = parser.parseExpression (
"#org == 'BTC'" ) .getValue ( stdContext, boolean . class ) ;
System.out.println ( resp ) ;
}
}
It gives the following output,
BTC
9999.0
true