Spring Expression Language (SpEL)

SpEL is powerful expression language created by Spring framework community to work with all the Spring framework products. There are several existing expression languages like MVEL, OGNL and JBoss EL. Every EL is developed with some purpose in mind. SpEL is created to help the Spring porfolio products.

The syntax used for the SpEL is same like Unified EL. But, it has lot of powerful features that are missing in the Unified EL.

Another advantage of SpEL is it can work independently without depending on the Spring environment. In the following sections, you will read the complete details on how tto write and use the Spring Expression Language.

Expression Evaluation using Spring's Expression Interface

The following code introduces the SpEL API to evaluate the literal string expression Hello World.

ExpressionParser parser = new SpelExpressionParser();
Expression exp =
 parser.parseExpression("'Hello World'");
String message = (String) exp.getValue();

The value of the message variable is simply Hello World.

The SpEL classes and interfaces you are most likely to use are located in the packages org.springframework.expression and its sub packages and spel.support.

The interface ExpressionParser is responsible for parsing an expression string.

In this example the expression string is a string literal denoted by the surrounding single quotes. The interface Expression is responsible for evaluating the previously defined expression string.

There are two exceptions that can be thrown, ParseException and EvaluationException when calling parser.parseExpression and exp.getValue respectively.

Example using xml based configuration

public class Order {

	private String item;
	private String price;
	private String address;

	public String getItem() {
		return item;
	}
	public void setItem(String item) {
		this.item = item;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}

package com.javasafari.beans;

public class PaymentGateway {

	private String itemName;
	
	private Order order;
	
	public void setOrder(Order ord){
		this.order = ord;
	}
	
	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}
 
	public Order getOrder() {
		return order;
	}

public void processOrder(){
System.out.println("Processing order:
"+order.getItem());
	}
}

Here is the xml based configuration file, the spring expression language is enclosed with in "#{ expression language }",watch below configuraton file closely. We are directly injecting values to the bean "beanOrder".

In the below xml, the expression language #{beanOrder} assigns "beanOrder" reference to order property, and #{beanOrder.item} assigns beanOrder item name to item property.

<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema
/beans/spring-beans-3.0.xsd">
<bean id="beanOrder"
 class="com.javasafari.beans.Order">
<property name="item" value="Java Book" />
<property name="price" value="Rs 22.50" />
<property name="address" value="India" />
 </bean>
 
<bean id="paymentGateway"
 class="com.javasafari.beans.PaymentGateway">
<property name="itemName" 
value="#{beanOrder.item}" />
<property name="order"
 value="#{beanOrder}" />
</bean>
</beans>

Spring demo class:

import org.springframework.context.
ConfigurableApplicationContext;
import org.springframework.context.support.
ClassPathXmlApplicationContext;

import com.javasafari.beans.PaymentGateway;

public class SpringDemo {

public static void main(String a[]){
String confFile = "applicationContext.xml";
ConfigurableApplicationContext context 
= new ClassPathXmlApplicationContext(confFile);
PaymentGateway gateway = (PaymentGateway) 
context.getBean("paymentGateway");
	gateway.processOrder();
	}
}
Processing order: Java Book

Spring EL example using annotations.


package com.javasafari.beans;
 
import org.springframework.beans.
factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component("beanOrder")
public class Order {
 
    @Value("Java Book")
    private String item;
     
    @Value("Rs 22.50")
    private String price;
     
    @Value("India")
    private String address;
 
    public String getItem() {
        return item;
    }
    public void setItem(String item) {
        this.item = item;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}
package com.javasafari.beans;
 
import org.springframework.beans.
factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component("paymentGateway")
public class PaymentGateway {
 
    @Value("#{beanOrder.item}")
    private String itemName;
     
    @Value("#{beanOrder}")
    private Order order;
     
    public void setOrder(Order ord){
        this.order = ord;
    }
     
    public String getItemName() {
        return itemName;
    }
 
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
  
    public Order getOrder() {
        return order;
    }
 
public void processOrder(){
System.out.println("Processing order:
 "+order.getItem());
    }
}

Here is the xml based configuration, you must enable component scan.

<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context=
"http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema
/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema
/context/spring-context-3.0.xsd">

<context:component-scan base-package=
"com.javasafari.beans" />
</beans>

Spring bean demo class



package com.javasafari.test;
 
import org.springframework.context.
ConfigurableApplicationContext;
import org.springframework.context.support.
ClassPathXmlApplicationContext;
 
import com.javasafari.beans.PaymentGateway;
 
public class SpringDemo {
 
public static void main(String a[]){
String confFile = "applicationContext.xml";
ConfigurableApplicationContext context
= new ClassPathXmlApplicationContext(confFile);
PaymentGateway gateway = (PaymentGateway) 
context.getBean("paymentGateway");
gateway.processOrder();
    }
}

Output
Processing order: Java Book