Share on Google+Share on Google+

SPEL-Wiring values from other bean properties

In this tutorial you will learn about spe Wiring values from other bean properties

SpEL: Wiring value from other bean properties

Spring 3 provides powerful Expression Language which can be used to wire values into beans properties.

Lets take an example to demonstrate how SpEL is used to wire value from other bean properties. The Product class contains property named parts which is of Set type.

package spel.wiringvaluesfromotherbeanproperties;

import java.util.*;

public class Product
private Set<String> parts;

public void setParts(Set<String> parts) { = parts;
public Set<String> getParts() {
return parts;
} This class contains the name of the part and will be used in configuration file to provide this name to the parts property of Product bean.

package spel.wiringvaluesfromotherbeanproperties;

import java.util.*;

public class Part
private String name;

public void setName(String name) { = name;
public String getName() {
return name;

spring-beans.xml: The <set> element is used to provide values to the Set type property of the Product class. Here <value> element provides those values. Here you can see the name of the parts are being provided using Spring expression language #{}, #{}, #{}. Here the value of the name property of each object of Part class is assigned to the parts property of the Product class.

<beans xmlns=""

<bean id="product" class="spel.wiringvaluesfromotherbeanproperties.Product">
<property name="parts">
<bean id="part1" class="spel.wiringvaluesfromotherbeanproperties.Part">
<property name="name" value="Part 1"/>
<bean id="part2" class="spel.wiringvaluesfromotherbeanproperties.Part">
<property name="name" value="Part 2"/>
<bean id="part3" class="spel.wiringvaluesfromotherbeanproperties.Part">
<property name="name" value="Part 3"/>
</beans> This class loads the context configuration file spring-beans-list.xml from the class path.

package spel.wiringvaluesfromotherbeanproperties;

import org.springframework.context.ApplicationContext;

import spel.wiringvaluesfromotherbeanproperties.Product;

public class AppMain{
public static void main( String[] args ){
ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {"spel\\wiringvaluesfromotherbeanproperties\\spring-beans-list.xml"});
Product product = (Product)appContext.getBean("product");

for (String part : product.getParts()) {

The output of the above program will be as below:

Part 1
Part 2
Part 3

Download Select Source Code


Posted on: April 7, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.