Spring Setter Injection with Collection
We can inject collection values by setter method in spring framework. There can be used three elements inside the property element.
It can be:
- list
- set
- map
In this example, we are using list that can have duplicate elements, set that have only unique elements and map that have both key and value.

CollectionEx.java
This class contains List,Set,Map and Properties with setters and getters method that prints the information.
package com.codeNuclear;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionEx
{
List addressList;
Set addressSet;
Map addressMap;
Properties addressProp;
public void setAddressList(List addressList)
{
this.addressList = addressList;
}
public List getAddressList()
{
System.out.println("List Elements :"+addressList);
return addressList;
}
public void setAddressSet(Set addressSet)
{
this.addressSet = addressSet;
}
public Set getAddressSet()
{
System.out.println("Set Elements :"+addressSet);
return addressSet;
}
public void setAddressMap(Map addressMap)
{
this.addressMap = addressMap;
}
public Map getAddressMap()
{
System.out.println("Map Elements :"+addressMap);
return addressMap;
}
public void setAddressProp(Properties addressProp)
{
this.addressProp = addressProp;
}
public Properties getAddressProp()
{
System.out.println("Properties Elements :"+addressProp);
return addressProp;
}
}
applicationContext.xml
Following is the configuration file applicationContext.xml which has configuration for all the type of collections.
<?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-3.0.xsd">
<!-- Definition for javaCollection -->
<bean id = "javaCollection" class = "com.codeNuclear.CollectionEx">
<!-- results in a setAddressList(java.util.List) call -->
<property name = "addressList">
<list>
<value>INDIA</value>
<value>Australia</value>
<value>USA</value>
<value>USA</value>
</list>
</property>
<!-- results in a setAddressSet(java.util.Set) call -->
<property name = "addressSet">
<set>
<value>INDIA</value>
<value>Australia</value>
<value>USA</value>
<value>USA</value>
</set>
</property>
<!-- results in a setAddressMap(java.util.Map) call -->
<property name = "addressMap">
<map>
<entry key = "1" value = "INDIA"/>
<entry key = "2" value = "Australia"/>
<entry key = "3" value = "USA"/>
<entry key = "4" value = "USA"/>
</map>
</property>
<!-- results in a setAddressProp(java.util.Properties) call -->
<property name = "addressProp">
<props>
<prop key = "one">INDIA</prop>
<prop key = "one">INDIA</prop>
<prop key = "two">Australia</prop>
<prop key = "three">USA</prop>
<prop key = "four">USA</prop>
</props>
</property>
</bean>
</beans>
TestApp.java
This class gets the bean from the applicationContext.xml file.
package com.codeNuclear;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class TestApp {
public static void main(String[] args)
{
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(r);
CollectionEx jc=(CollectionEx)factory.getBean("javaCollection");
jc.getAddressList();
jc.getAddressSet();
jc.getAddressMap();
jc.getAddressProp();
}
}
Output

