Spring Tutorial Home

  • Managing Database Transactions
  • Remoting in Spring
  • Working with the Web Layer

Spring Home

Java Resources

Community

Site

"http://www.w3.org/TR/html4/loose.dtd">









Wiring Collections


The most commonly and widely used API of the java is collection. All the software uses some of the classes of collectgion to organize their objects in the application. The Spring also provides the means by which we can wire the collection to the properties of the other objects at the time of object creation. The following are the tags given by spring to wire various collection interfaces:-



  1. <list> Wiring a list of values, allowing duplicates.

  2. <set> Wiring a set of values, ensuring no duplicates

  3. <map> Wiring a collection of name-value pairs where name and value can be of any type

  4. <props> Wiring a collection of name-value pairs where the name and value are both Strings


Examples :- The following declarations will demonstrate the various collection wirings.


Lets take an example bean which has Set, Map and List collection properties:-




package com.visualbuilder.beans;


import java.util.List;

import java.util.Map;

import java.util.Set;


public class CollectionInjectionBean {

   
List products;

   
Set productSet;

   
Map prices;

  public Map getPrices() {

        return prices;

   
}

  public void setPrices(Map prices) {

       
this.prices = prices;

   
}

  public List getProducts() {

        return products;

   
}

  public void setProducts(List products) {

       
this.products = products;

   
}

  public Set getProductSet() {

        return productSet;

   
}

  public void setProductSet(Set productSet) {

       
this.productSet = productSet;

   
}



}


 





<?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-2.5.xsd">


    <bean id="collectionExample" class="com.visualbuilder.beans.CollectionInjectionBean">

       
<property name="products">

           
<list>

               
<value>CAR</value>

               
<value>MOTORCYCLE</value>

               
<value>SCOOTER</value>

           
</list>

       
</property>

       
<property name="productSet">

           
<set>

               
<value>CAR</value>

               
<value>MOTORCYCLE</value>

               
<value>SCOOTER</value>

           
</set>

       
</property>



       
<property name="prices">

           
<map>

               
<entry key="CAR" value="100" />

               
<entry key="MOTORCYCLE" value="200" />

               
<entry key="SCOOTER" value="10" />

           
</map>

       
</property>

   
</bean>


</beans>

ColletionInjectionExample.java




package com.visualbuilder.example;


import java.util.ArrayList;

import java.util.Map;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.visualbuilder.beans.CollectionInjectionBean;


public class ColletionInjectionExample {

    public static void main (String [] ar){

               
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"com/visualbuilder/xml/spring-beans.xml"});

               
CollectionInjectionBean bean = (CollectionInjectionBean)context.getBean("collectionExample");

               
ArrayList products=(ArrayList) bean.getProducts();

               
Map prices= bean.getPrices();

        for (int i =0;i< products.size();i++){

                       
String productName= (String)products.get(i);

            System.out.println("Price of "+productName+" is "+ prices.get(productName) );

               
}

       
}

}

Output :-


Jun 1, 2008 2:08:13 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Sun Jun 01 14:08:13 IST 2008]; root of context hierarchy

Jun 1, 2008 2:08:13 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/visualbuilder/xml/spring-beans.xml]

Jun 1, 2008 2:08:13 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@15e83f9

Jun 1, 2008 2:08:13 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15e83f9: defining beans [collectionExample]; root of factory hierarchy


Price of CAR is 100

Price of MOTORCYCLE is 200

Price of SCOOTER is 10




                    

Copyright © 2010 VisualBuilder. All rights reserved