Spring Collections (List, Set, Map, and Properties) example
Spring examples to show you how to inject values into collections type (List, Set, Map, and Properties). 4 major collection types are supported :
- List – <list/>
- Set – <set/>
- Map – <map/>
- Properties – <props/>
Spring beans
A Customer object, with four collection properties.
package com.mkyong.common;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Customer
{
private List<Object> lists;
private Set<Object> sets;
private Map<Object, Object> maps;
private Properties pros;
//...
}
See different code snippets to declare collection in bean configuration file.
1. List example
<property name="lists">
<list>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.mkyong.common.Person">
<property name="name" value="mkyongList" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</list>
</property>
2. Set example
<property name="sets">
<set>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.mkyong.common.Person">
<property name="name" value="mkyongSet" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</set>
</property>
3. Map example
<property name="maps">
<map>
<entry key="Key 1" value="1" />
<entry key="Key 2" value-ref="PersonBean" />
<entry key="Key 3">
<bean class="com.mkyong.common.Person">
<property name="name" value="mkyongMap" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</entry>
</map>
</property>
4. Properties example
<property name="pros">
<props>
<prop key="admin">admin@nospam.com</prop>
<prop key="support">support@nospam.com</prop>
</props>
</property>
Full Spring’s bean configuration 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-2.5.xsd">
<bean id="CustomerBean" class="com.mkyong.common.Customer">
<!-- java.util.List -->
<property name="lists">
<list>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.mkyong.common.Person">
<property name="name" value="mkyongList" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</list>
</property>
<!-- java.util.Set -->
<property name="sets">
<set>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.mkyong.common.Person">
<property name="name" value="mkyongSet" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</set>
</property>
<!-- java.util.Map -->
<property name="maps">
<map>
<entry key="Key 1" value="1" />
<entry key="Key 2" value-ref="PersonBean" />
<entry key="Key 3">
<bean class="com.mkyong.common.Person">
<property name="name" value="mkyongMap" />
<property name="address" value="address" />
<property name="age" value="28" />
</bean>
</entry>
</map>
</property>
<!-- java.util.Properties -->
<property name="pros">
<props>
<prop key="admin">admin@nospam.com</prop>
<prop key="support">support@nospam.com</prop>
</props>
</property>
</bean>
<bean id="PersonBean" class="com.mkyong.common.Person">
<property name="name" value="mkyong1" />
<property name="address" value="address 1" />
<property name="age" value="28" />
</bean>
</beans>
Run it…
package com.mkyong.common;
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 cust = (Customer)context.getBean("CustomerBean");
System.out.println(cust);
}
}
Output
Customer [
lists=[
1,
Person [address=address 1, age=28, name=mkyong1],
Person [address=address, age=28, name=mkyongList]
],
maps={
key 1=1,
key 2=Person [address=address 1, age=28, name=mkyong1],
key 3=Person [address=address, age=28, name=mkyongMap]
},
pros={admin=admin@nospam.com, support=support@nospam.com},
sets=[
1,
Person [address=address 1, age=28, name=mkyong1],
Person [address=address, age=28, name=mkyongSet]]
]
SpringCollectionExample
Spring ListFactoryBean example
By mkyong - March 15, 2010 spring
The ‘ListFactoryBean‘ class provides developer a way to create a concrete List collection class (ArrayList and LinkedList) in Spring’s bean configuration file.
Here’s a ListFactoryBean example, it will instantiate an ArrayList at runtime, and inject it into a bean property.
package com.mkyong.common;
import java.util.List;
public class Customer
{
private List lists;
//...
}Spring’s bean configuration 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-2.5.xsd">
<bean id="CustomerBean" class="com.mkyong.common.Customer">
<property name="lists">
<bean class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="targetListClass">
<value>java.util.ArrayList</value>
</property>
<property name="sourceList">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>
</property>
</bean>
</beans>Alternatively, you also can use util schema and <util:list> to achieve the same thing.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean id="CustomerBean" class="com.mkyong.common.Customer">
<property name="lists">
<util:list list-class="java.util.ArrayList">
<value>1</value>
<value>2</value>
<value>3</value>
</util:list>
</property>
</bean>
</beans>Remember to include the util schema, else you will hit the following error
Caused by: org.xml.sax.SAXParseException:
The prefix "util" for element "util:list" is not bound.Run it…
package com.mkyong.common;
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 cust = (Customer) context.getBean("CustomerBean");
System.out.println(cust);
}
}Ouput
Customer [lists=[1, 2, 3]] Type=[class java.util.ArrayList]You have instantiated ArrayList and injected it into Customer’s lists property at runtime.
Spring SetFactoryBean example
By mkyong - March 16, 2010 spring
The ‘SetFactoryBean‘ class provides developer a way to create a concrete Set collection (HashSet and TreeSet) in Spring’s bean configuration file.
Here’s a ListFactoryBean example, it will instantiate an HashSet at runtime, and inject it into a bean property
package com.mkyong.common;
import java.util.Set;
public class Customer
{
private Set sets;
//...
}Spring’s bean configuration 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-2.5.xsd">
<bean id="CustomerBean" class="com.mkyong.common.Customer">
<property name="sets">
<bean class="org.springframework.beans.factory.config.SetFactoryBean">
<property name="targetSetClass">
<value>java.util.HashSet</value>
</property>
<property name="sourceSet">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
</bean>
</property>
</bean>
</beans>Alternatively, you also can use util schema and <util:set> to achieve the same thing.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean id="CustomerBean" class="com.mkyong.common.Customer">
<property name="sets">
<util:set set-class="java.util.HashSet">
<value>1</value>
<value>2</value>
<value>3</value>
</util:set>
</property>
</bean>
</beans>Remember to include the util schema, else you will hit the following error
Caused by: org.xml.sax.SAXParseException:
The prefix "util" for element "util:set" is not bound.Run it…
package com.mkyong.common;
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 cust = (Customer)context.getBean("CustomerBean");
System.out.println(cust);
}
}Ouput
Customer [sets=[3, 2, 1]] Type=[class java.util.HashSet]You have instantiated HashSet and and injected it into Customer’s sets property at runtime.
Spring MapFactoryBean example
By mkyong - March 17, 2010 spring
The ‘MapFactoryBean‘ class provides developer a way to create a concrete Map collection class (HashMap and TreeMap) in Spring’s bean configuration file.
Here’s a MapFactoryBean example, it will instantiate a HashMap at runtime,, and inject it into a bean property.
package com.mkyong.common;
import java.util.Map;
public class Customer
{
private Map maps;
//...
}Spring’s bean configuration 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-2.5.xsd">
<bean id="CustomerBean" class="com.mkyong.common.Customer">
<property name="maps">
<bean class="org.springframework.beans.factory.config.MapFactoryBean">
<property name="targetMapClass">
<value>java.util.HashMap</value>
</property>
<property name="sourceMap">
<map>
<entry key="Key1" value="1" />
<entry key="Key2" value="2" />
<entry key="Key3" value="3" />
</map>
</property>
</bean>
</property>
</bean>
</beans>Alternatively, you also can use util schema and <util:map> to achieve the same thing.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean id="CustomerBean" class="com.mkyong.common.Customer">
<property name="maps">
<util:map map-class="java.util.HashMap">
<entry key="Key1" value="1" />
<entry key="Key2" value="2" />
<entry key="Key3" value="3" />
</util:map>
</property>
</bean>
</beans>Remember to include the util schema, else you will hit the following error
Caused by: org.xml.sax.SAXParseException:
The prefix "util" for element "util:map" is not bound.Run it…
package com.mkyong.common;
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 cust = (Customer)context.getBean("CustomerBean");
System.out.println(cust);
}
}Ouput
Customer [maps={Key2=2, Key1=1, Key3=3}] Type=[class java.util.HashMap]You have instantiated a HashMap and injected it into Customer’s map property at runtime.