Spring Collection Injection

    0 Votes

In our previous examples, we have seen that we can inject single value using value attribute. We had also seen that we can inject a Bean using ref attribute. But what about the Collection attribute or plural values?

Spring allow us to pass plural value, so it has four different types of attributes by which we can pass Collection values. Here are the list of elements.

TypeDescription
  <list> By this tag, we can inject list values. As you know list accepts duplicate so it allows duplicate.
  <set> By this tag, we can inject set values. As you know set does not accept duplicate so it does not allow duplicate.
  <map> By this tag, we can inject name value pair. As you know map accepts name /value pair and it can be any data type.
 <props> By this tag, we can inject name value pair but here both are Strings.
Now we will take an Example where we can use all the collection Elements.

We will take a simple example of Person where I declare person’s visited place as List. Hobby as Set, Education as Map and relative as Properties.

Collection.xml

We define a bean called Person , It has four collection peroperties

  • Visited Place : which is list , Please note that, as visitedPlace is a list it contains duplicate value.
  • hobbySet : which is Set. Please note that as hobbySet is a Set, although we provide duplicate values but on outcome it contains only unique values.
  • educationMap : which is a Map. Please note that we set education standards as Key and organization name as a value.
  • relativeProperty : Which is Property, Property is special type of Map whose key and Value are String. Here we treat relationship as Key and name as a value.

Collection.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-3.0.xsd">

   <bean id="person" class="com.example.collection.Person">	 
  	<property name="visitedPlaceList">
     	<list>
        	<value>Delhi</value>
        	<value>Mumbai</value>
        	<value>Kolkata</value>
        	<value>Kolkata</value>
     	</list>
  	</property>

  	<property name="hobbySet">
     	<set>
        	<value>Photography</value>
        	<value>Surfing</value>
        	<value>Rafting</value>
        	<value>Rafting</value>
      	</set>
  	</property>

  	<property name="educationMap">
     	<map>
        	<entry key="10" value="Scottish Chruch Scool"/>
        	<entry key="12" value="BD GOVT School"/>
        	<entry key="14" value="Techno India"/>
      	</map>
  	</property>
 	 
  	
  	<property name="relativeProperty">
     	<props>
        	<prop key="Father">Samir Kumar Mitra</prop>
        	<prop key="Mother">Late Subhra Mitra</prop>
        	<prop key="Spouse">Swatika Mitra</prop>
    	</props>
  	</property>
   </bean>
</beans>

Person Java Class

package com.example.collection;

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

public class Person {
    
    List visitedPlaceList;
    Set hobbySet;
    Map<Integer,String> educationMap;
    Properties relativeProperty;
    public List getVisitedPlaceList() {
   	 return visitedPlaceList;
    }
    public void setVisitedPlaceList(List visitedPlaceList) {
   	 this.visitedPlaceList = visitedPlaceList;
    }
    public Set getHobbySet() {
   	 return hobbySet;
    }
    public void setHobbySet(Set hobbySet) {
   	 this.hobbySet = hobbySet;
    }
    public Map<Integer, String> getEducationMap() {
   	 return educationMap;
    }
    public void setEducationMap(Map<Integer, String> educationMap) {
   	 this.educationMap = educationMap;
    }
    public Properties getRelativeProperty() {
   	 return relativeProperty;
    }
    public void setRelativeProperty(Properties relativeProperty) {
   	 this.relativeProperty = relativeProperty;
    }
    @Override
    public String toString() {
   	 return "Person [visitedPlaceList=" + visitedPlaceList + ", hobbySet="
   			 + hobbySet + ", educationMap=" + educationMap
			 + ", relativeProperty=" + relativeProperty + "]";
    }
}

Main.java class

package com.example.collection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    
public static void main(String[] args) {  	 
   	 ApplicationContext ctx = new ClassPathXmlApplicationContext("configFiles/collection.xml");
   	 
   	 Person person =(Person) ctx.getBean("person");
   	 System.out.println(person);
    }
}

Person [visitedPlaceList=[Delhi, Mumbai, Kolkata, Kolkata], hobbySet=[Photography, Surfing, Rafting], educationMap={10=Scottish Chruch Scool, 12=BD GOVT School, 14=Techno India}, relativeProperty={Father=Samir Kumar Mitra, Mother=Late Subhra Mitra, Spouse=Swatika Mitra}]

Please note that we are doing setter injection here. Also in hobbySet, we have provided duplicate value but it takes unique values.

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

Spring Framework - Overview
Spring Architecture
Step by step guide for Spring configuration
Building First Spring application
Spring Bean XML File
Spring Container
Spring Bean - Learn with an Example
Spring Bean Scope
Spring Bean Life Cycle
Spring Bean Post Processor
Spring Inner Bean
Spring Autowiring
Spring Autowiring Types
Spring Qualifier with Autowiring
Spring Inheritance
Spring Event Handling
Dependency Injection
Spring AOP
Spring AOP Example