Spring Collection Injection Example with List, Set, Map and Properties

By Arvind Rai, March 23, 2023
On this page we will provide Spring collection injection example with List, Set, Map and Properties. To populate values in the collection, Spring provides different elements as follows.

1. <list> is for List
2. <set> is for Set
3. <map> is for Map
4. <props> is for Properties

Spring Collection List Injection

Spring provides <list> tag to inject java List using Spring application context XML. <list> is used within <property> or <constructor-arg>. To add values in the List, Spring provides <value> that is nested within <list>.
      <property name="myList">
	  <list>
	       <value>List Value A</value>
	       <value>List Value B</value>
	  </list>
      </property> 

Spring Collection Set Injection

Spring provides <set> tag to inject java Set using Spring application context XML. <set> is used within <property> or <constructor-arg>. To add values in the Set, Spring provides <value> that is nested within <set>.
      <property name="mySet">
          <set>
            <value>Set Value A</value>
            <value>Set Value B</value>
          </set>
      </property> 

Spring Collection Map Injection

Spring provides <map> tag to inject java Map using Spring application context XML. <map> is used within <property> or <constructor-arg>. To add values in the Map, Spring provides <entry> that is nested within <map>.
      <property name="myMap">
	 <map>
            <entry key="0" value="Map Value A"/>           
            <entry key="1" value="Map Value B"/>
         </map>
      </property> 

Spring Collection Properties Injection

Spring provides <props> tag to inject java Properties using Spring application context XML. <props> is used within <property> or <constructor-arg>. To add values in the Properties, Spring provides <prop> that is nested within <props>.
      <property name="myProp">
	<props>
	    <prop key="propKeyA">Prop Value A</prop>
	    <prop key="propKeyB">Prop Value B</prop>
	</props>
      </property>     

Collection Injection with <ref>

To inject collection of objects to our beans can also be achieved by <ref>. We can provide reference of bean in our collection element population.
      <property name="bookList">
      	<list>
      	    <ref bean="book1"/>
      	    <ref bean="book2"/>
      	</list>
      </property> 

Complete Example

Now we will provide complete example for Spring collection injection. Here we will provide collection injection using setter based dependency injection as well as constructor based dependency injection.

Collection Injection using <property>

spring-config-1.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="myCollection" class="com.concretepage.bean.MyCollection">
      <property name="myList">
	  <list>
	       <value>List Value A</value>
	       <value>List Value B</value>
	  </list>
      </property>
      <property name="mySet">
          <set>
            <value>Set Value A</value>
            <value>Set Value B</value>
          </set>
      </property>
      <property name="myMap">
	 <map>
            <entry key="0" value="Map Value A"/>           
            <entry key="1" value="Map Value B"/>
         </map>
      </property>
      <property name="myProp">
	<props>
	    <prop key="propKeyA">Prop Value A</prop>
	    <prop key="propKeyB">Prop Value B</prop>
	</props>
      </property>      
      <property name="bookList">
      	<list>
      	    <ref bean="book1"/>
      	    <ref bean="book2"/>
      	</list>
      </property>
    </bean>
    <bean id="book1" class="com.concretepage.bean.Book">
    	<property name="bookId" value="100"/>
    	<property name="bookName" value="Godan"/>
    </bean>
    <bean id="book2" class="com.concretepage.bean.Book">
    	<property name="bookId" value="200"/>
    	<property name="bookName" value="Nirmla"/>
    </bean>    
</beans> 

Collection Injection using <constructor-arg>

spring-config-2.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="myCollection" class="com.concretepage.bean.MyCollection">
      <constructor-arg name="myList">
	  <list>
	       <value>List Value A</value>
	       <value>List Value B</value>
	  </list>
      </constructor-arg>
      <constructor-arg name="mySet">
        <set>
            <value>Set Value A</value>
            <value>Set Value B</value>
        </set>
      </constructor-arg>
      <constructor-arg name="myMap">
	 <map>
            <entry key="0" value="Map Value A"/>           
            <entry key="1" value="Map Value B"/>
         </map>
      </constructor-arg>
      <constructor-arg name="myProp">
	 <props>
	       <prop key="propKeyA">Prop Value A</prop>
	       <prop key="propKeyB">Prop Value B</prop>
	 </props>
      </constructor-arg>      
      <constructor-arg name="bookList">
      	<list>
      		<ref bean="book1"/>
      		<ref bean="book2"/>
      	</list>
      </constructor-arg>
    </bean>
    <bean id="book1" class="com.concretepage.bean.Book">
    	<property name="bookId" value="100"/>
    	<property name="bookName" value="Godan"/>
    </bean>
    <bean id="book2" class="com.concretepage.bean.Book">
    	<property name="bookId" value="200"/>
    	<property name="bookName" value="Nirmla"/>
    </bean>    
</beans> 

Create Beans

MyCollection.java
package com.concretepage.bean;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class MyCollection {
	private List<String> myList;
	private Set<String> mySet;
	private Map<Integer,String> myMap;	
	private Properties myProp;	
	private List<Book> bookList;
	public MyCollection () {}
	public MyCollection(List<String> myList, Set<String> mySet, Map<Integer,String> myMap, 
			Properties myProp, List<Book> bookList) {
		this.myList = myList;
		this.mySet = mySet;		
		this.myMap = myMap;
		this.myProp = myProp;
		this.bookList = bookList;
	}
	public List<String> getMyList() {
		return myList;
	}
	public void setMyList(List<String> myList) {
		this.myList = myList;
	}
	public Set<String> getMySet() {
		return mySet;
	}
	public void setMySet(Set<String> mySet) {
		this.mySet = mySet;
	}
	public Map<Integer, String> getMyMap() {
		return myMap;
	}
	public void setMyMap(Map<Integer, String> myMap) {
		this.myMap = myMap;
	}
	public Properties getMyProp() {
		return myProp;
	}
	public void setMyProp(Properties myProp) {
		this.myProp = myProp;
	}
	public List<Book> getBookList() {
		return bookList;
	}
	public void setBookList(List<Book> bookList) {
		this.bookList = bookList;
	}
} 
Book.java
package com.concretepage.bean;
public class Book {
	private int bookId;
	private String bookName;
	public int getBookId() {
		return bookId;
	}
	public void setBookId(int bookId) {
		this.bookId = bookId;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
} 

Run Application

SpringDemo.java
package com.concretepage;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.concretepage.bean.MyCollection;
public class SpringDemo {
	public static void main(String[] args) {
		AbstractApplicationContext  context = new ClassPathXmlApplicationContext("spring-config-1.xml");
        MyCollection myCollection=(MyCollection)context.getBean("myCollection");
        //access list
        System.out.println("---access list---");        
        List<String> list=myCollection.getMyList();
        System.out.println(list.get(0));
        System.out.println(list.get(1));
        //access set
        System.out.println("---access set---");
        Set<String> set=myCollection.getMySet();
        Iterator<String> itr= set.iterator();
        while(itr.hasNext()){
        	System.out.println(itr.next());
        }
        //access map
        System.out.println("---access map---");
        Map<Integer,String> map=myCollection.getMyMap();
        System.out.println(map.get(0));
        System.out.println(map.get(1));
        //access properties
        System.out.println("---access properties---");
        Properties prop=myCollection.getMyProp();
        System.out.println(prop.getProperty("propKeyA"));
        System.out.println(prop.getProperty("propKeyB"));        
        //Access Book List
        System.out.println("---Access Book List---");
        System.out.println(myCollection.getBookList().get(0).getBookName());
        System.out.println(myCollection.getBookList().get(1).getBookName());
        context.close();
    } 
} 
Find the output.
---access list---
List Value A
List Value B
---access set---
Set Value A
Set Value B
---access map---
Map Value A
Map Value B
---access properties---
Prop Value A
Prop Value B
---Access Book List---
Godan
Nirmla 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us