Spring EL hello world example

The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation time. In addition, all Spring expressions are available via XML or annotation.

In this tutorial, we show you how to use Spring Expression Language(SpEL), to inject String, integer and bean into property, both in XML and annotation.

1. Spring EL Dependency

Declares the core Spring jars in Maven pom.xml file, it will download the Spring EL dependencies automatically.

File : pom.xml


	<properties>
		<spring.version>3.0.5.RELEASE</spring.version>
	</properties>

	<dependencies>
	
		<!-- Spring 3 dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
	
	<dependencies>

2. Spring Beans

Two simple beans, later use SpEL to inject values into property, in XML and annotation.


package com.mkyong.core;

public class Customer {

	private Item item;

	private String itemName;

}

package com.mkyong.core;

public class Item {

	private String name;

	private int qty;

}

3. Spring EL in XML

The SpEL are enclosed with #{ SpEL expression }, see following example in XML bean definition file.


<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="itemBean" class="com.mkyong.core.Item">
		<property name="name" value="itemA" />
		<property name="qty" value="10" />
	</bean>

	<bean id="customerBean" class="com.mkyong.core.Customer">
		<property name="item" value="#{itemBean}" />
		<property name="itemName" value="#{itemBean.name}" />
	</bean>
	
</beans>
  1. #{itemBean} – inject “itemBean” into “customerBean” bean’s “item” property.
  2. #{itemBean.name} – inject “itemBean”‘s “name” property into “customerBean” bean’s “itemName” property.

4. Spring EL in Annotation

See equivalent version in annotation mode.

Note
To use SpEL in annotation, you must register your component via annotation. If you register your bean in XML and define @Value in Java class, the @Value will failed to execute.

package com.mkyong.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer {

	@Value("#{itemBean}")
	private Item item;

	@Value("#{itemBean.name}")
	private String itemName;

	//...

}

package com.mkyong.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("itemBean")
public class Item {

	@Value("itemA") //inject String directly
	private String name;

	@Value("10") //inject interger directly
	private int qty;

	public String getName() {
		return name;
	}

	//...
}

Enable auto component scanning.


<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.mkyong.core" />

</beans>

In annotation mode, you use @Value to define Spring EL. In this case, you inject a String and Integer value directly into the “itemBean“, and later inject the “itemBean” into “customerBean” property.

5. Output

Run it, both SpEL in XML and annotation are display the same result :


package com.mkyong.core;

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

public class App {
	public static void main(String[] args) {
	    ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");

	    Customer obj = (Customer) context.getBean("customerBean");
	    System.out.println(obj);
	}
}

Output


Customer [item=Item [name=itemA, qty=10], itemName=itemA]

Download Source Code

Download It – Spring3-EL-Hello-Worldr-Example.zip (6 KB)

Reference

  1. Spring EL reference

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
16 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
14 Comment authors
venkata raghavaRamjimbobPuneeth ShivalingaiahMak Recent comment authors
newest oldest most voted
Ram
Guest
Ram

Can some one please let me know how to access ArrayList of Arraylist using spring Expression Language?

I have Pojo that has a Arraylist of Customer POJO which in turn has a ArrayList of Address POJO.

I tried using the ‘Projection’ operator and tried something like Customers.![Addresses.![addressFirstLine]]

but getting error. Help appreciated

venkata raghava
Guest
venkata raghava

hi i am getting this error can pls help me any one
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 5): Field or property ‘name’ cannot be found on object of type ‘beans.Item’

jimbob
Guest
jimbob

I can’t seem to get this to work. Question: Does spel not require the spring-expression maven dependency? I havent tried that yet, but wondering if that’s why it’s not working for me.

Puneeth Shivalingaiah
Guest
Puneeth Shivalingaiah

Concise and simple :)

Mak
Guest
Mak

Thanks

Imran Lakhani
Guest
Imran Lakhani

Setters and getter methods for Item and customer are required to make it work.
Also the toString method for Customer is also needed for getting desired result.

Anil
Guest
Anil

How do i implement this in a spring batch… i tried this but since its standalone its not working.. please help

guru
Guest
guru

Extremely helpful, Me and my collegues are regular users of your site. Keep up the good work.

Eric Seidler
Guest
Eric Seidler

Custom .toString methods are required to produce the output listed in the example. Perhaps that is implied or I missed something.

for Customer.java:

@Override
public String toString() {
return String.format(“Customer [item=%s, itemName=%s]”,item.toString(), getItemName());
}

for Item.java:

@Override
public String toString() {
return String.format(“Item [name=%s, qty=%d]”, getName(), getQty());
}

SudhirR
Guest
SudhirR

I meant to ask

is the same as

Right?

SudhirR
Guest
SudhirR

is the same result as

right?

Ishant
Guest
Ishant

can we use spring expression language in jsp like #{object.property}

remember object already present in context factory

trackback
Spring Tutorial

[…] Spring EL hello world example Quick start to use the Spring expression language (EL). […]

Satish Kumar
Guest
Satish Kumar

Could you please let me know how to set values in collections like List,Set,Map using this @Value annotation?

MoroccoGuy
Guest
MoroccoGuy

why did you use in the annotation equivalent example @component rather than @bean ? and what’s the difference between the two annotaions ?

gayath
Guest
gayath
trackback
SPEL! I conquered you on a Friday! « Pure-Essence.Net

[…] So I looked into SPEL. […]