How to inject various collections into spring beans will be covered in this tutorial. Following are the common collection usages in spring framework

1) List
2) Set
3) Map
4) Map<Integer,List>

Step 1: Spring Beans and their configuration

User.java

package com.codesimplify.spring.collections;
public class User {	 
	private String firstName;
	private String lastName;
	private Integer age;
         //Removed Setter Getter to save space
}

SimpleSpringCollection.java

package com.codesimplify.spring.collections;

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

public class SimpleSpringCollection {	
	private List userList;	
	private Map<Integer,User> userMap;	
	private Set userSet;
         private Map<String,List> deptUserList;
         //Removed Setter Getter to save space
}

Spring Configuration for List Example.

<property name="userList">
   <list>
    <bean class="com.codesimplify.spring.collections.User">
     <property name="firstName" value="James"/>
     <property name="lastName" value="Wilson" />
     <property name="age" value="30" />
    </bean>
    <ref bean="adminUserBean" />
   </list>
  </property>
 <bean id="adminUserBean" class="com.codesimplify.spring.collections.User">
   <property name="firstName" value="James-admin" />
   <property name="lastName" value="Wagnor" />
 </bean>

Spring Configuration for Set Example.

<property name="userSet">
 <set>    
   <bean class="com.codesimplify.spring.collections.User">
  <property name="firstName" value="karen-Set" />
  <property name="lastName" value="Smith" />
  <property name="age" value="30" />
   </bean>    
   <!-- We are referring existing bean using ref -->
   <ref bean="adminUserBean"/>
 </set>
  </property>

Spring Configuration for Map Example.

<property name="userMap">
 <map>
  <entry key="1" >  <!-- represents ID -->
   <bean class="com.codesimplify.spring.collections.User">
    <property name="firstName" value="karen-Map" />
    <property name="lastName" value="Smith" />
    <property name="age" value="28" />
   </bean>
  </entry>
  <!-- We are referring value of key="2" to existing bean using value-ref -->
  <entry key="2" value-ref="adminUserBean"/>
 </map>
</property>

Spring Configuration for Map<Integer,List> Example.

<property name="deptUserList">
<map>
<entry key="1" >  <!-- represents ID -->
<list>
<bean class="com.codesimplify.spring.collections.User">
<property name="firstName" value="James"/>
<property name="lastName" value="Wilson" />
<property name="age" value="30" />
</bean>
<ref bean="adminUserBean" />
</list>
</entry>
</map>
</property>

Full Spring configuration file covering above examples

application-collections.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.xsd"> 
 <bean id="simpleSpringCollection" class ="com.codesimplify.spring.collections.SimpleSpringCollection">
  <property name="userList">
   <list>
    <bean class="com.codesimplify.spring.collections.User">
     <property name="firstName" value="James"/>
     <property name="lastName" value="Wilson" />
     <property name="age" value="30" />
    </bean>
    <ref bean="adminUserBean" />
   </list>
  </property>  
  <property name="userMap">
   <map>
    <entry key="1" >  <!-- represents ID -->
     <bean class="com.codesimplify.spring.collections.User">
      <property name="firstName" value="karen-Map" />
      <property name="lastName" value="Smith" />
      <property name="age" value="28" />
     </bean>
    </entry>
    <!-- We are referring value of key="2" to existing bean using value-ref -->
    <entry key="2" value-ref="adminUserBean"/>
   </map>
  </property>  
  <property name="userSet">
   <set>    
    <bean class="com.codesimplify.spring.collections.User">
     <property name="firstName" value="karen-Set" />
     <property name="lastName" value="Smith" />
     <property name="age" value="30" />
    </bean>    
    <!-- We are referring existing bean using ref -->
    <ref bean="adminUserBean"/>
   </set>
  </property>  
  <property name="deptUserList">
   <map>
    <entry key="1" >  <!-- represents ID -->
     <list>
      <bean class="com.codesimplify.spring.collections.User">
       <property name="firstName" value="James"/>
       <property name="lastName" value="Wilson" />
       <property name="age" value="30" />
      </bean>
      <ref bean="adminUserBean" />
     </list>
    </entry>    
   </map>
  </property>
 </bean>
 <bean id="adminUserBean" class="com.codesimplify.spring.collections.User">
   <property name="firstName" value="James-admin" />
   <property name="lastName" value="Wagnor" />
 </bean>
</beans>

Step 2: Java Class that executes and prints the above configuration


package com.codesimplify.spring.collections;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SimpleSpringCollectionsTest {
	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("application-collections.xml");
		SimpleSpringCollection sample=(SimpleSpringCollection)context.getBean("simpleSpringCollection");
		System.out.println(sample);
	}
}

Output


list: userList -
[firstName: James,lastName:Wilson,Age:30
, firstName: James-admin,lastName:Wagnor,Age:null
] ,
 Map:userMap - 
{1=firstName: karen-Map,lastName:Smith,Age:28
, 2=firstName: James-admin,lastName:Wagnor,Age:null
}
 Set:userSet - 
[firstName: karen-Set,lastName:Smith,Age:30
, firstName: James-admin,lastName:Wagnor,Age:null
]
 Map UserListByDept - 
{1=[firstName: James,lastName:Wilson,Age:30
, firstName: James-admin,lastName:Wagnor,Age:null
]}
Spring Framework – Collections
Tagged on:         

One thought on “Spring Framework – Collections

  • September 27, 2015 at 10:20 pm
    Permalink

    Thanks for your simple example.it helped me understand.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *