Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring Collections Examples

    • 0
    • 1
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 392
    Comment on it

    Spring supports collection framework, the list of collections Spring framework supports are List, Set, Map, and Properties. Spring allow us to inject the values at the time of bean definition in XML configuration file.

    Tags used to inject collections are as follows:

    List : <list></list>
    Set : <set></set>
    Map : <map></map>
    Properties : <props></props>
    

    Following is the example of spring collections, that shows, how collections can be injected in spring bean. Address.java

    package com.evon.example.collections;
    public class Address 
    {
        private String city;
        private String country;
    
        //getter and setters for all of the above fields..
    }
    

    Student.java

    package com.evon.example.collections; 
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;
    import com.evon.example.collections.Address; 
    public class Student 
    {
        private List<Object> studentList;
        private Set<Object> studentSet;
        private Map<Object, Object> studentMap;
        private Properties studentProps;
    
        //getter and setters for all of the above fields..
    }
    

    Spring XML configuration for List:

    <property name="studentList">
            <list>
                <value>10</value>
                <ref bean="addressBean" />
                <bean class="com.evon.example.collections.Address">
                    <property name="city" value="Dehradun" />
                    <property name="country" value="India" />
                </bean>
            </list>
    </property>
    

    Spring XML configuration for Set:

    <property name="studentSet">
            <set>
                <value>12</value>
                <ref bean="addressBean" />
                <bean class="com.evon.example.collections.Address">
                    <property name="city" value="Dehradun" />
                    <property name="country" value="India" />
                </bean>
            </set>
        </property>
    

    Spring XML configuration for Map:

    <property name="studentMap">
            <map>
                <entry key="Key 1" value="14" />
                <entry key="Key 2" value-ref="addressBean" />
                <entry key="Key 3">
                    <bean class="com.evon.example.collections.Address">
                    <property name="city" value="Dehradun" />
                    <property name="country" value="India" />
                </bean>
                </entry>
            </map>
        </property>
    

    Spring XML configuration for Properties:

    <property name="studentProps">
            <props>
                <prop key="adminMail">admin@evontech.com</prop>
                <prop key="supportMail">support@evontech.com</prop>
            </props>
        </property>
    

    Complete spring bean configuration file :

    ApplicationContext.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="studentBean" class="com.evon.example.collections.collections.Student">
    
            <!-- java.util.List -->
            <property name="studentList">
                <list>
                    <value>10</value>
                    <ref bean="addressBean" />
                    <bean class="com.evon.example.collections.Address">
                        <property name="city" value="Dehradun" />
                        <property name="country" value="India" />
                    </bean>
                </list>
            </property>
    
            <!-- java.util.Set -->
            <property name="studentSet">
                <set>
                    <value>12</value>
                    <ref bean="addressBean" />
                    <bean class="com.evon.example.collections.Address">
                        <property name="city" value="Dehradun" />
                        <property name="country" value="India" />
                    </bean>
                </set>
            </property>
    
            <!-- java.util.Map -->
            <property name="studentMap">
                <map>
                    <entry key="Key 1" value="14" />
                    <entry key="Key 2" value-ref="addressBean" />
                    <entry key="Key 3">
                        <bean class="com.evon.example.collections.Address">
                            <property name="city" value="Dehradun" />
                            <property name="country" value="India" />
                        </bean>
                    </entry>
                </map>
            </property>
    
            <!-- java.util.Properties -->
            <property name="studentProps">
                <props>
                    <prop key="adminMail">admin@evontech.com</prop>
                    <prop key="supportMail">support@evontech.com</prop>
                </props>
            </property>
        </bean> 
        <bean id="addressBean" class="com.evon.example.collections.Address">
            <property name="city" value="Delhi" />
            <property name="country" value="India" />
        </bean>
    </beans>
    

    Main.java

    package com.evon.example.collections; 
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main 
    {
        public static void main( String[] args )
        {
            ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    
            Student student = (Student)context.getBean("studentBean");
            System.out.println(student); 
        }
    }
    

    Output:

    Student [
    studentList=[
    10, 
    Address [city=Delhi, country=India], 
    Address [city=Dehradun, country=India]
    ], 
    
    studentMap={
    key 1=14,
    key 2=Address [city=Delhi, country=India], 
    key 3=Address [city=Dehradun, country=India]
    }, 
    
    studentProps={adminMail=admin@evontech.com, supportMail=support@evontech.com}, 
    
    studentSet=[
    12, 
    Address [city=Delhi, country=India], 
    Address [city=Dehradun, country=India]
    ]
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: