Home Tutorial Spring Spring3 Web SPEL-Wiring values from other bean properties

 
 

Share on Google+Share on Google+
SPEL-Wiring values from other bean properties
Posted on: April 7, 2011 at 12:00 AM
Advertisement
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.

Product.java: 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) {
this.parts = parts;
}
public Set<String> getParts() {
return parts;
}
}

Part.java: 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) {
this.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 #{part1.name}, #{part2.name}, #{part3.name}. 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="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-2.5.xsd">

<bean id="product" class="spel.wiringvaluesfromotherbeanproperties.Product">
<property name="parts">
<set>
<value>#{part1.name}</value>
<value>#{part2.name}</value>
<value>#{part3.name}</value>
</set>
</property>
</bean>
<bean id="part1" class="spel.wiringvaluesfromotherbeanproperties.Part">
<property name="name" value="Part 1"/>
</bean>
<bean id="part2" class="spel.wiringvaluesfromotherbeanproperties.Part">
<property name="name" value="Part 2"/>
</bean>
<bean id="part3" class="spel.wiringvaluesfromotherbeanproperties.Part">
<property name="name" value="Part 3"/>
</bean>
</beans>

AppMain.java: This class loads the context configuration file spring-beans-list.xml from the class path.

package spel.wiringvaluesfromotherbeanproperties;

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

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()) {
System.out.println(part);
}
}
}

The output of the above program will be as below:

Part 1
Part 2
Part 3

Download Select Source Code

Advertisement

Related Tags for SPEL-Wiring values from other bean properties:


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.

Posted on: April 7, 2011

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com