Tuesday 24 December 2013

Spring Collections

You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.
Now what about if you want to pass plural values like Java Collection types List, Set, Map, and Properties. To handle the situation, Spring offers four types of collection configuration elements which are as follows:



ElementDescription
<list>This helps in wiring ie injecting a list of values, allowing duplicates.
<set>This helps in wiring a set of values but without any duplicates.
<map>This can be used to inject a collection of name-value pairs where name and value can be of any type.
<props>This can be used to inject a collection of name-value pairs where the name and value are both Strings.

You will come across two situations (a) Passing direct values of the collection and (b) Passing a reference of a bean as one of the collection elements.

Example:

Here is the content of Customer.java file:

package com.mahesh.spring;

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

public class Customer {

    private List<Object> lists;
    private Set<Object> sets;
    private Map<Object, Object> maps;
    private Properties pros;

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public Set<Object> getSets() {
        return sets;
    }

    public void setSets(Set<Object> sets) {
        this.sets = sets;
    }

    public Map<Object, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<Object, Object> maps) {
        this.maps = maps;
    }

    public Properties getPros() {
        return pros;
    }

    public void setPros(Properties pros) {
        this.pros = pros;
    }

    @Override
    public String toString() {
        return "Customer [lists=" + lists + ", sets=" + sets + ", maps=" + maps
                + ", pros=" + pros + "]";
    }

}

Here is the content of Person.java file:

package com.mahesh.spring;

public class Person {

    String name;
    String address;
    int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
   
    @Override
    public String toString() {
        return "Person [address=" + address + ", age=" + age + ", name=" + name + "]";
    }

}

Here is the content of App.java file:

 package clientpack;

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

import com.mahesh.Customer;

public class App {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "SpringBeans.xml");

        Customer1 cust = (Customer1) context.getBean("CustomerBean");
        System.out.println(cust);
    }

}

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

SpringBeans.xml  


<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="CustomerBean" class="com.mahesh.Customer">

        <!-- java.util.List -->
        <property name="lists">
            <list>
                <value>1</value>
                <ref bean="PersonBean" />
                <bean class="com.mahesh.Person">
                    <property name="name" value="maheshl" />
                    <property name="address" value="address" />
                    <property name="age" value="20" />
                </bean>
            </list>
        </property>

        <!-- java.util.Set -->
        <property name="sets">
            <set>
                <value>1</value>
                <ref bean="PersonBean" />
                <bean class="com.mahesh.Person">
                    <property name="name" value="maheshs" />
                    <property name="address" value="address" />
                    <property name="age" value="20" />
                </bean>
            </set>
        </property>

        <!-- java.util.Map -->
        <property name="maps">
            <map>
                <entry key="Key 1" value="1" />
                <entry key="Key 2" value-ref="PersonBean" />
                <entry key="Key 3">
                    <bean class="com.mahesh.Person">
                        <property name="name" value="maheshm" />
                        <property name="address" value="address" />
                        <property name="age" value="28" />
                    </bean>
                </entry>
            </map>
        </property>

        <!-- java.util.Properties -->
        <property name="pros">
            <props>
                <prop key="email">mahesh.chegu@gmail.com</prop>
            </props>
        </property>

    </bean>

    <bean id="PersonBean" class="com.mahesh.Person">
        <property name="name" value="mahesh" />
        <property name="address" value="address 1" />
        <property name="age" value="20" />
    </bean>
   
</beans>

OutPut:

Customer [lists=[1, Person [address=address 1, age=20, name=mahesh], Person [address=address, age=20, name=maheshl]], sets=[1, Person [address=address 1, age=20, name=mahesh], Person [address=address, age=20, name=maheshs]], maps={Key 1=1, Key 2=Person [address=address 1, age=20, name=mahesh], Key 3=Person [address=address, age=28, name=maheshm]}, pros={email=mahesh.chegu@gmail.com}]


No comments:

Post a Comment