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

Constructors 

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 creating a new instance by invoking constructor using new operator.

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

import java.util.Map;

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

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

public class ConstructorTest {

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

    ExpressionParser parser = new SpelExpressionParser();
    Employee emp = parser.parseExpression(
      "new com.bethecoder.tutorials.spring3.basic.Employee(1, 'ABC', 9999)"
    ).getValue(Employee.class);
    
    System.out.println(emp);
    
    String value = parser.parseExpression("new String('ABCDEFG')").getValue(String.class);
    System.out.println(value);
    
    Map simpleMap = parser.parseExpression("new java.util.HashMap()").getValue(Map.class);
    System.out.println(simpleMap.getClass());

    int size = parser.parseExpression("new java.util.HashMap().size()").getValue(int.class);
    System.out.println(size);

    boolean isEmpty = parser.parseExpression("new java.util.ArrayList().isEmpty()").getValue(boolean.class);
    System.out.println(isEmpty);
    
  }
}
   

It gives the following output,
Employee[1, ABC, 9999.0]
ABCDEFG
class java.util.HashMap
0
true



 
  


  
bl  br