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

Variable 

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 variables using SpEL.

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

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 VariableTest {

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

    StandardEvaluationContext stdContext = new StandardEvaluationContext();
    //Define a variable named 'org' with value 'BTC'
    stdContext.setVariable("org""BTC");
    stdContext.setVariable("emp"new Employee(1"ABC"9999));
    
    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



 
  


  
bl  br