Pages

Saturday, July 31, 2010

Collections in Spring

The major collection types are supported :
List –  <list/>
Set –  <set/>
Map –  <map/>
Properties –  <props/>



Here are a list of the collection type examples in Spring’s bean configuration file, the constant value is define by <value>, bean reference by <ref>, and inner bean definition by <bean>.



applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd">

<!-- Employeebean used for a few examples -->
<bean id="employee" class="com.Employee">
<property name="name" value="Vardhan"></property>
<property name="id" value="31413"></property>
</bean>

<!-- collection injection samples -->
<bean id="injectCollection" class="com.CollectionInjection">
<!-- Map Example -->
<property name="map">
<map>
<entry key="key 1">
<value>Map Example</value>
</entry>
<entry key="key 2">
<ref local="employee"/>
</entry>
<entry key="key 3">
<bean class="com.Employee">
<property name="name" value="Name_Map" />
<property name="id" value="1" />
</bean>
</entry>
</map>
</property>
<!-- Properties Example -->
<property name="props">
<props>
<prop key="Key 1"> Properties_Name </prop>
<prop key="Key 2"> Properties_Id </prop>
</props>
</property>
<!-- Set Example -->
<property name="set">
<set>
<value>Set Example</value>
<ref local="employee"/>
<bean class="com.Employee">
<property name="name" value="Name_Set" />
<property name="id" value="2" />
</bean>
</set>
</property>
<!-- List Example -->
<property name="list">
<list>
<value>List Example</value>
<ref local="employee"/>
<bean class="com.Employee">
<property name="name" value="Name_List" />
<property name="id" value="3" />
</bean>
</list>
</property>
</bean>
</beans>


Employee.java
package com;
/**
 * @author vardhan
 *
 */
public class Employee  {

private String name;
private int id;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
}


CollectionInjection.java

package com;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class CollectionInjection {
private Map map;
private Properties props;
private Set set;
private List list;
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"src\\com\\applicationContext.xml"));

CollectionInjection instance = (CollectionInjection) factory
.getBean("injectCollection");
instance.displayInfo();
}

public void setList(List list) {
this.list = list;
}

public void setSet(Set set) {
this.set = set;
}

public void setMap(Map map) {
this.map = map;
}

public void setProps(Properties props) {
this.props = props;
}

public void displayInfo() {
// display the Map
Iterator i = map.keySet().iterator();
System.out.println("Map contents:\n");
while (i.hasNext()) {
Object key = i.next();
if(map.get(key) instanceof Employee) {
Employee emp = (Employee) map.get(key);
System.out.println("Key: " + key + " - Name : " + emp.getName()+" - Id : " + emp.getId());
}else
System.out.println("Key: " + key + " - Value: " + map.get(key));
}

// display the properties
i = props.keySet().iterator();
System.out.println("\nProperties contents:\n");
while (i.hasNext()) {
String key = i.next().toString();
System.out.println("Key: " + key + " - Value: "
+ props.getProperty(key));
}

// display the set
i = set.iterator();
System.out.println("\nSet contents:\n");
while (i.hasNext()) {
Object key = i.next();
if (key instanceof Employee) {
Employee emp = (Employee) key;
System.out.println("Name : " + emp.getName()+" - Id : " + emp.getId());
}else{
String name = (String)key;
System.out.println("Value : "+name);
}
}

// display the list
i = list.iterator();
System.out.println("\nList contents:\n");
while (i.hasNext()) {
Object key = i.next();
if (key instanceof Employee) {
Employee emp = (Employee) key;
System.out.println("Name : " + emp.getName()+" - Id : " + emp.getId());
}else{
String name = (String)key;
System.out.println("Value : "+name);
}
}
}
}




Output

Map contents:

Key: key 1 - Value: Map Example
Key: key 2 - Name : Vardhan - Id : 31413
Key: key 3 - Name : Name_Map - Id : 1

Properties contents:

Key: Key 2 - Value: Properties_Id
Key: Key 1 - Value: Properties_Name

Set contents:

Value : Set Example
Name : Vardhan - Id : 31413
Name : Name_Set - Id : 2

List contents:

Value : List Example
Name : Vardhan - Id : 31413
Name : Name_List - Id : 3



Note *** Out put of the Properties collection type is executed from down to up like 
Key: Key 2 - Value: Properties_Id
Key: Key 1 - Value: Properties_Name

1 comment: