Reading:  

Quick to the point introduction to Spring Framework


Injecting Collection

Spring provides a few types of configuration collection elements which are used to pass plural values like Java Collection types List, Map, Set and Properties.

The major types of collection are:

  • List - <list/>: This helps in injecting a values list, it allows duplicate values.
  • Set - <set/>: This helps in injecting a values set but it will not allow any duplicate values.
  • Map - <map/>: This helps to inject a pairs of collection name-value. Here the name-value can be any type.
  • Properties - <props/>: This helps to inject a pairs of collection name-value. Here the name and value are both Strings.

Example:

Java file: Student

package pack1;
import java.util.*;

public class Student {
   List nameList;
   Set  nameSet;
   Map  nameMap;
   Properties nameProp;

  public void setNameList(List nameList) {
     this.nameList = nameList;
   }
  public List getNameList() {
      System.out.println("List contains :"  + nameList);
      return nameList;
   }
  public void setNameSet(Set nameSet) {
      this.nameSet = nameSet;
   }
   public Set getNameSet() {
      System.out.println("Set contains :"  + nameSet);
      return nameSet;
   }
   public void setNameMap(Map nameMap) {
      this.nameMap = nameMap;
   }
   public Map getNameMap() {
      System.out.println("Map contains :"  + nameMap);
      return nameMap;
   }
  public void setNameProp(Properties nameProp) {
  this.nameProp = nameProp;
   }
  public Properties getNameProp() {
  System.out.println("Property contains :"  + nameProp);
  return nameProp;
   }
}

 

Java file: SampleTest

package pack1;

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

public class SampleTest {
   public static void main(String[] args) {
      ApplicationContext cont = 
             new ClassPathXmlApplicationContext("Beans.xml");

      Student st = (Student)cont.getBean("collBean");

      st.getNameList();
      st.getNameSet();
      st.getNameMap();
      st.getNameProp();
   }
}
 

Bean.xml file

<?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=" collBean " class="pack1.Student">

      <!-- results in a setNameList(java.util.List) call -->
      <property name="nameList">
        <list>
           <value>Nimrit</value>
           <value>Yuvi</value>
           <value>KK</value>
           <value>Craig</value>
           <value>Craig</value>
        </list>
      </property>

     <!-- results in a setNameSet(java.util.Set) call -->
     <property name="nameSet">
        <set>
           <value>Nimrit</value>
           <value>Yuvi</value>
           <value>KK</value>
           <value>Craig</value>
           <value>Craig</value>
          </set>
      </property>

     <!-- results in a setNameMap(java.util.Map) call -->
     <property name="nameMap">
        <map>
           <entry key="2" value="Nimrit"/>
           <entry key="4" value="Yuvi"/>
           <entry key="1" value="KK"/>
           <entry key="3" value="Craig"/>
          <entry key="5" value="Craig"/>
        </map>
      </property>

     <!-- results in a setNameProp(java.util.Properties) call -->
     <property name="nameProp">
        <props>
           <prop key="two">Nimrit</prop>
           <prop key="four">Yuvi</prop>
           <prop key="one">KK</prop>
           <prop key="three">Craig</prop>
        </props>
      </property>

   </bean>
</beans>
 


Compile and run the program will get the below result:


Result:


List contains :[Nimrit, Yuvi, KK, Craig, Craig]
Set contains :[ Nimrit, Yuvi, KK, Craig]
Map contains :{ 2=Nimrit, 4=Yuvi, 1=KK, 3=Craig, 5=Craig}
Property contains :{two= Nimrit, four=Yuvi, one=KK, three=Craig, five=Craig }

Injecting Bean References:

User can also inject bean references as one of the collection's element and can mix values and references together.

Example:

<?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="..." class="...">

      <!-- Passing bean reference  for java.util.List -->
      <property name="nameList">
        <list>
           <ref bean="name1"/>
           <ref bean="name2"/>
           <value>Anoop</value>
        </list>
      </property>

     <!-- Passing bean reference  for java.util.Map -->
     <property name="nameMap">
        <map>
           <entry key="two" value="Suman"/>
           <entry key ="one" value-ref="name1"/>
           <entry key ="three" value-ref="name2"/>
        </map>
      </property>

   </bean>
</beans>

Injecting null and empty string values:

To pass an empty string as a value:

<bean id="..." class="…">
   <property name="ContactNum" value=""/>
</bean>

To pass a null as a value:

<bean id="..." class="…">
   <property name=" ContactNum”><null/></property>
</bean>

 

 

Description

This tutorial covers various topics releated to Spring Framework as listed below

  • Framework Overview
  • Modules
  • Environment Setup
  • Saying Hello World with Spring
  • IoC Containers
  • Bean Definition
  • Bean Scopes 
  • Bean Life Cycle
  • Bean Post Processors
  • Bean Definition Inheritance
  • Dependency Injection
  • Injecting Inner Beans
  • Injecting Collection
  • Beans Auto-Wiring
  • Annotation Based Configuration
  • Java Based Configuration
  • Event Handling in Spring
  • Custom Events in Spring
  • JDBC Framework Overview
  • Transaction Management

 

 



Prerequisites

Prior knowledge of Java is essential

Audience

Beginners or students seeking quick introduction to Spring

Learning Objectives

This tutorial is for beginners seeking quick introduction to Spring Framework.

Author: Subject Coach
Added on: 22nd Jun 2015

You must be logged in as Student to ask a Question.

None just yet!