Spring Collections (List, Set, Map, And Properties)

Spring offers four types of collection configuration elements that come in handy when configuring collections of values.

Table 1.1 Collection types
Collection Equivalent Element Description
List <list> Wiring a list of values, allowing duplicates
Set <set> Wiring a set of values, ensuring no duplicates
Map <map> Wiring a collection of name-value pairs where name and value
can be of any type
Properties <props> Wiring a collection of name-value pairs where the name and
value are both Strings

1. List

The list element will create a list that can be injected into a constructor or property that takes a java.util.List.

List Element

    <bean id="employee" class="com.kruders.bean.Employee">
	   <property name="name" value="Puneet" />
	   <property name="lists">
       	<list>
           <bean class="com.kruders.bean.Address">
				<property name="city" value="Delhi" />
				<property name="country" value="India" />
		   </bean>
		   <bean class="com.kruders.bean.Address">
				<property name="city" value="Banglore" />
				<property name="country" value="India" />
		   </bean>
        </list>
      </property>
   </bean>

Click Here for source code.

List Factory

Simple factory for shared List instances. Allows for central setup of Lists via the “list” element in XML bean definitions.

    <bean id="employee" class="com.kruders.bean.Employee">
	   <property name="name" value="Puneet" />
	   <property name="list">
			<bean class="org.springframework.beans.factory.config.ListFactoryBean">
				<property name="targetListClass">
					<value>java.util.ArrayList</value>
				</property>
				<property name="sourceList">
					<list>
						<bean class="com.kruders.bean.Address">
							<property name="city" value="Delhi" />
							<property name="country" value="India" />
						</bean>
					</list>
				</property>
			</bean>
	   </property>
    </bean>

Click Here for source code.

List Util

Another custom namespace utility for creating lists. You can also explicitly control the exact type of List that will be instantiated and populated via the use of the util schema.

    <bean id="employee" class="com.kruders.bean.Employee">
	   <property name="name" value="Puneet" />
	   <property name="list">
			<util:list list-class="java.util.ArrayList">
				<bean class="com.kruders.bean.Address">
					<property name="city" value="Delhi" />
					<property name="country" value="India" />
				</bean>
			</util:list>
    	   </property>
    </bean>

Click Here for source code.

2. Set

The set element will create a java.util.Set that can be injected into a constructor or property. Using a Set is identical to using a List except a Set will only have unique values.

Set Element

<bean id="employee" class="com.kruders.bean.Employee">
    <property name="name" value="Puneet" />
    <property name="set">
    <set>
        <bean class="com.kruders.bean.Address">
             <property name="city" value="Delhi" />
             <property name="country" value="India" />
        </bean>
        <bean class="com.kruders.bean.Address">
             <property name="city" value="Banglore" />
             <property name="country" value="India" />
        </bean>
    </set>
    </property>
</bean>

Click Here for source code.

Set Factory

Simple factory for shared Set instances. Allows for central setup of Sets via the “set” element in XML bean definitions.

<bean id="employee" class="com.kruders.bean.Employee">
       <property name="name" value="Puneet" />
       <property name="set">
            <bean class="org.springframework.beans.factory.config.SetFactoryBean">
                <property name="targetSetClass">
                    <value>java.util.HashSet</value>
                </property>
                <property name="sourceSet">
                    <set>
                        <bean class="com.kruders.bean.Address">
                            <property name="city" value="Delhi" />
                            <property name="country" value="India" />
                        </bean>
                    </set>
                </property>
            </bean>
       </property>
   </bean>

Click Here for source code.

Set Util

Another custom namespace utility for creating sets. You can also explicitly control the exact type of Set that will be instantiated and populated via the use of the util schema.

	<bean id="employee" class="com.kruders.bean.Employee">
		<property name="name" value="Puneet" />
		<property name="set">
			<util:set set-class="java.util.HashSet">
				<bean class="com.kruders.bean.Address">
					<property name="city" value="Delhi" />
					<property name="country" value="India" />
				</bean>
			</util:set>
		</property>
	</bean>

Click Here for source code.

3. Map

The map element will create a java.util.Map that can be injected into a constructor or property. The map element expects an entry element inside it to define key/value pairs for the Map.

Map Element

    <bean id="employee" class="com.kruders.bean.Employee">
       <property name="name" value="Puneet" />
       <property name="maps">
        <map>
           <entry key="1">
                <bean class="com.kruders.bean.Address">
                    <property name="city" value="Delhi" />
                    <property name="country" value="India" />
                </bean>
           </entry>
           <entry key="2">
               <bean class="com.kruders.bean.Address">
                    <property name="city" value="Banglore" />
                    <property name="country" value="India" />
               </bean>
           </entry>
        </map>
       </property>
   </bean>

Click Here for source code.

Map Factory

Simple factory for shared Map instances. Allows for central setup of Maps via the “map” element in XML bean definitions.

    <bean id="employee" class="com.kruders.bean.Employee">
       <property name="name" value="Puneet" />
       <property name="maps">
            <bean class="org.springframework.beans.factory.config.MapFactoryBean">
                <property name="targetMapClass">
                    <value>java.util.HashMap</value>
                </property>
                <property name="sourceMap">
                    <map>
                        <entry key="1">
                            <bean class="com.kruders.bean.Address">
                                <property name="city" value="Delhi" />
                                <property name="country" value="India" />
                            </bean>
                        </entry>
                    </map>
                </property>
            </bean>
       </property>
   </bean>

Click Here for source code.

Map Util

Another custom namespace utility for creating maps. You can also explicitly control the exact type of Map that will be instantiated and populated via the use of the util schema.

    <bean id="employee" class="com.kruders.bean.Employee">
       <property name="name" value="Puneet" />
       <property name="maps">
            <util:map map-class="java.util.HashMap">
                <entry key="1">
                    <bean class="com.kruders.bean.Address">
                        <property name="city" value="Delhi" />
                        <property name="country" value="India" />
                    </bean>
                </entry>
            </util:map>
       </property>
   </bean>

Click Here for source code.

4. Properties

The props element will create a java.util.Properties that can be injected into a constructor or property. A property key/value pair can be added to the props element using the prop element.

Props Element

	<bean id="employee" class="com.kruders.bean.Employee">
	   <property name="name" value="Puneet" />
	   <property name="phone">
		<props>
			<prop key="Home">01123232322</prop>
			<prop key="Mobile">9239232922</prop>
		</props>
	  </property>
   </bean>

Click Here for source code.

Properties Factory

Simple factory for shared Properties instances. Allows for central setup of Properties via the “props” element in XML bean definitions.

    <bean id="employee" class="com.kruders.bean.Employee">
       <property name="name" value="Puneet" />
       <property name="phone">
            <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
                <property name="properties">
                    <props>
                        <prop key="Home">01123232322</prop>
                        <prop key="Mobile">9239232922</prop>
                    </props>
                </property>
            </bean>
       </property>
   </bean>

Click Here for source code.

Properties Util

Another custom namespace utility for creating properties. It uses Spring FactoryBean implementation, the PropertiesFactoryBean, to instantiate a java.util.Properties instance with values loaded from the supplied Resource location.

    <bean id="employee" class="com.kruders.bean.Employee">
       <property name="name" value="Puneet" />
       <property name="phone">
            <util:properties>
                <prop key="Home">01123232322</prop>
                <prop key="Mobile">9239232922</prop>
            </util:properties>
       </property>
   </bean>

Click Here for source code.

5. Injecting Bean References

You can also inject bean references as one of the collection’s element. You can also put bean reference and values together.

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

	<bean id="employee" class="com.kruders.bean.Employee">
	   <property name="name" value="Puneet" />
	   <property name="lists">
       	<list>
           <ref bean="address"/>
           <bean class="com.kruders.bean.Address">
				<property name="city" value="Banglore" />
				<property name="country" value="India" />
		   </bean>
        </list>
      </property>
   </bean>
   <bean id="address" class="com.kruders.bean.Address">
		<property name="city" value="Delhi" />
		<property name="country" value="India" />
   </bean>
</beans>

6. Wiring nothing (null)

Some bean may themselves set a property to a non-null default value. So, for reason if you want that property to be null, you must explicit wire null to that property. So, Spring can wire a null for you.

<propertyname="someNonNullProperty"><null/></property>

,

No comments yet.

Leave a Reply