Injecting Collections in Spring Application

Injecting Collections in Spring-We can inject collection values by constructor in spring framework. 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).

Injecting Collections in Spring

@ImageSource-SlideShare.net

You have seen how to configure primitive data type using value attribute and object references using ref attribute of the tag in your Bean configuration file(spring.xml). Both the cases deal with passing singular value to a bean. Now we will discuss about passing multiple value of data like List, Set, Collection, Map, Properties etc. To handle the situation, Spring offers four types of collection configuration element.

Injecting Collections in the Spring Application

There are different tags are using by these different Collection elements as follows:
  • List  — using– <list> – </list>
  • Set   — using– <set> – </set>
  • Map — using– <map> – </map>
  • Properties –using– <props> – </props>

Using List

Lets see that code snip sett how to use the List in the spring bean configuration file.
In this code snipset list contain all values tags only.
<property name="listOfShape">
   <list>
      <value>Triangle</value>
      <value>Circle</value>
      <value>Rectangle</value>
      <value>Square</value>
   </list>
</property>
In this code snipset list contain values and beans and inner bean tags.
<property name="listOfShape">
   <list>
      <value>Triangle</value>
      <value>Circle</value>
      <ref bean="rectangle"></ref>      
      <bean class="com.dineshonjava.sdnext.Square">
          <property name="width" value="20"></property>
      </bean>
    </list>
</property>

Using Set

Lets see that code snip sett how to use the Set in the spring bean configuration file.
In this code snipset set contain all values tags only.
<property name="setOfShape">
   <set>
      <value>Triangle</value>
      <value>Circle</value>
      <value>Rectangle</value>
      <value>Square</value>
   </set>
</property>
In this code snipset set contain values and beans and inner bean tags.
<property name="setOfShape">
   <set>
      <value>Triangle</value>
      <value>Circle</value>
      <ref bean="rectangle"></ref>      
      <bean class="com.dineshonjava.sdnext.Square">
          <property name="width" value="20"></property>
      </bean>
   </set>
</property>

Using Map

Lets see that code snip sett how to use the Map in the spring bean configuration file.
In this code snipset mapcontain all values tags only.
<property name="mapOfShape">
   <map>
     <entry key="1" value="Triangle">
     <entry key="2" value="Circle">
     <entry key="3" value="Rectangle">
     <entry key="4" value="Square">
     </entry></entry></entry></entry>
   </map>
</property>
In this code snipset map contain values and beans and inner bean tags.
<property name="mapOfShape">
   <map>
     <entry key="1" value="Triangle"></entry>
     <entry key="2" value="Circle"></entry>
     <entry key="3" value-ref="rectangle"></entry>
     <entry key="4">
        <bean class="com.dineshonjava.sdnext.Square">
          <property name="width" value="20"></property>
        </bean>
     </entry>
   </map>
</property>

Using Properties file

Lets see that code snip sett how to use the Properties file in the spring bean configuration file.
<property name="proprtyOfShape">
   <props>
     <prop key="triangle">Triangle</prop>
     <prop key="circle">Circle</prop>
     <prop key="rectangle">Rectangle</prop>
     <prop key="sqare">Square</prop>
   </props>
</property>
Now See the full example with the Value injecting to Collection.

ShapeCollection.java

package com.dineshonjava.sdnext.injectingCollection;

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

public class ShapeCollection {
 private List<string>        shapeOfList;
 private Set<string>         shapeOfSet;
 private Map<string string=""> shapeOfMap;
 private Properties          shapeOfProperties;
 /**
  * @param shapeOfList the shapeOfList to set
  */
 public void setShapeOfList(List<string> shapeOfList) {
  this.shapeOfList = shapeOfList;
 }
 /**
  * @param shapeOfSet the shapeOfSet to set
  */
 public void setShapeOfSet(Set<string> shapeOfSet) {
  this.shapeOfSet = shapeOfSet;
 }
 /**
  * @param shapeOfMap the shapeOfMap to set
  */
 public void setShapeOfMap(Map<string string=""> shapeOfMap) {
  this.shapeOfMap = shapeOfMap;
 }
 /**
  * @param shapeOfProperties the shapeOfProperties to set
  */
 public void setShapeOfProperties(Properties shapeOfProperties) {
  this.shapeOfProperties = shapeOfProperties;
 }
 
 public String toString()
 {
return "List Elements :"  + shapeOfList+"nSet Elements :"  + shapeOfSet+"n" +
 "Map Elements :"  + shapeOfMap+"nProperty Elements :"  + shapeOfProperties;
  
 }

}
Following is the configuration file spring.xml which has configuration for all the type of collections:

spring.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   
   <bean class="com.dineshonjava.sdnext.injectingCollection.ShapeCollection" id="shapeCollection">

      
      <property name="shapeOfList">
        <list>
           <value>Triangle</value>
           <value>Circle</value>
           <value>Circle</value>
           <value>Rectangle</value>
        </list>
      </property>

     
     <property name="shapeOfSet">
        <set>
           <value>Triangle</value>
           <value>Circle</value>
           <value>Circle</value>
           <value>Rectangle</value>
        </set>
      </property>

     
     <property name="shapeOfMap">
        <map>
           <entry key="1" value="Triangle">
           <entry key="2" value="Circle">
           <entry key="3" value="Circle">
           <entry key="4" value="Rectangle">
        </entry></entry></entry></entry></map>
      </property>

     
     <property name="shapeOfProperties">
        <props>  
      <prop key="triangle">Triangle</prop>  
      <prop key="circle1">Circle</prop>  
      <prop key="circle2">Circle</prop>  
      <prop key="rectangle">Rectangle</prop>  
    </props> 
      </property>

   </bean>

</beans>

DrawingApp.java

package com.dineshonjava.sdnext.injectingCollection.tutorial;

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

import com.dineshonjava.sdnext.injectingCollection.ShapeCollection;


/**
 * @author Dinesh Rajput
 *
 */
public class DrawingApp 
{
 /**
  * @param args
  */
 public static void main(String[] args) 
 {
  ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  ShapeCollection shapeCollection = (ShapeCollection) context.getBean("shapeCollection");
  System.out.println(shapeCollection);
 }
}
Once you are done with creating source and bean configuration files, let us run the application. If everything is fine with your application, this will print the following message on console:
Output:
Jun 24, 2012 3:50:42 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ab50cd: startup date [Sun Jun 24 15:50:42 IST 2012]; root of context hierarchy
Jun 24, 2012 3:50:42 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Jun 24, 2012 3:50:43 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1fc2fb: defining beans [shapeCollection]; root of factory hierarchy

List Elements :[Triangle, Circle, Circle, Rectangle]
Set Elements :[Triangle, Circle, Rectangle]
Map Elements :{1=Triangle, 2=Circle, 3=Circle, 4=Rectangle}
Property Elements :{rectangle=Rectangle, circle2=Circle, triangle=Triangle, circle1=Circle}


Injecting Bean References to the Collection

In the following bean configuration file has understand how to inject bean references as one of the collection’s element.

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean class="com.dineshonjava.sdnext.injectCollection.Triangle" id="triangle">
       <property name="height" value="30"></property>
       <property name="type" value="Eqiletral"></property>
   </bean>

   <bean class="com.dineshonjava.sdnext.injectingCollection.ShapeCollection" id="shapeCollection">

     
      <property name="shapeOfList">
        <list>
           <value>Circle</value>
           <value>Circle</value>
           <ref bean="triangle"></ref>
           <bean class="com.dineshonjava.sdnext.injectCollection.Rectangle">
              <property name="height" value="30"></property>
              <property name="width" value="50"></property>
           </bean>
        </list>
      </property>

     
     <property name="shapeOfSet">
        <set>
           <value>Circle</value>
           <value>Circle</value>
           <ref bean="triangle"></ref>
           <bean class="com.dineshonjava.sdnext.injectCollection.Rectangle">
              <property name="height" value="30"></property>
              <property name="width" value="50"></property>
           </bean>
        </set>
      </property>

     
     <property name="shapeOfMap">
        <map>
           <entry key="2" value="Circle"></entry>
           <entry key="3" value="Circle"></entry>
           <entry key="1" ref-value="triangle"></entry>
           <entry key="4">
              <bean class="com.dineshonjava.sdnext.injectCollection.Rectangle">
                 <property name="height" value="30"></property>
                 <property name="width" value="50"></property>
               </bean>
           </entry>
        </map>
      </property>

     
     <property name="shapeOfProperties">
        <props>  
          <prop key="triangle">Triangle</prop>  
          <prop key="circle1">Circle</prop>  
          <prop key="circle2">Circle</prop>  
          <prop key="rectangle">Rectangle</prop>  
        </props> 
      </property>

   </bean>

</beans>
Spring Related Topics you may like

 

 

Previous
Next