Wednesday, June 26, 2013

Spring Collections Using Util XML tags.

    In this post I am going to explain about Spring Util tags, how to use. Spring frameworks provides Util schema to configure Collections in spring bean configuration file. The util tags deal with common, utility configuration issues, such as configuring collections.
To use the tags in the util schema, you need to have the following preamble at the top of your Spring XML configuration file (bold ones).

you can also refer my previous post to learn SpringCore and SpringAutowire

<?xml version="1.0" encoding="UTF-8"?>
<beans
      xmlns="http://www.springframework.org/schema/beans"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-3.0.xsd">

      <!—Your bean configurations -->
</bean>

Here Is my spring bean configuration file. Here I have configured 4 collection beans with the ids list, set, map and properties.

list:  will hold the list of items, internally spring container will create java.util.ArrayList object and set the values into indexes.

set: will hold the set of items, internally spring container will create java.util.LinkedHashSet object and set the values into indexes.

            Both List and Set have <value>…</value> as child element in it’s to pass the values.

map: will hold the key-value pairs so we have to provide both key and values in your spring configuration file using the child element <entry key=”” value=”” />. Internally spring container will create java.util.LinkedHashMap object with the specified key value pairs.

properties: will hold the key = value pairs so we have to provide key value pairs in spring configuration file using the child element <prop key=”” >…</prop>. Internally spring container will create java.util.Properties object with the specified key=value pairs.


Here I configured one more bean with the name “sampleCollection” and I am using autowireby Name to auto inject all the above collection beans into my SampleCollection class.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
      xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
                                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                                    http://www.springframework.org/schema/util
                                    http://www.springframework.org/schema/util/spring-util-3.0.xsd">
      <util:list id="list">
             <value>listItem1</value>
             <value>listItem2</value>
             <value>listItem3</value>
      </util:list>
     
      <util:set id="set">
            <value>setItem1</value>
            <value>setItem2</value>
            <value>setItem3</value>
      </util:set>
     
      <util:map id="map">
            <entry key="mapKey1" value="mapValue1" />
            <entry key="mapKey2" value="mapValue2" />
            <entry key="mapKey3" value="mapValue3" />
      </util:map>
       
      <util:properties id="properties" scope="singleton">
            <prop key="propKey1">
                  PropValue1
            </prop>
            <prop key="propKey2">
                  PropValue2
            </prop>
      </util:properties>
     
      <bean id="sampleCollection" class="com.lova.spring.util.SampleCollection" autowire="byName" />
     
      <!-- or injecting through xml configuration-->
      <!--
      <bean id="sampleCollection1" class="com.lova.spring.util.SampleCollection">
                  <property name="list" ref="list" />
                  <property name="set" ref="set" />
                  <property name="map" ref="map" />
                  <property name="properties" ref="properties" />
    </bean>
    -->
     
</beans>

SampleCollection.java

package com.lova.spring.util;

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

/**
 * @author Lovababu
 *
 */
public class SampleCollection
{

      private List<String> list;
     
      private Set<String> set;
     
      private Map<String, String> map;
     
      private Properties properties;

      // Generate setter and getter method before run the test app.
     
}

SampleCollectionTest.java

package com.lova.spring.util.test;

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

import com.lova.spring.util.SampleCollection;

/**
 * @author Lovababu
 *
 */
public class SampleCollectionTest
{

      public static void main(String[] args)
{
           
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
           
            SampleCollection sampleCollection = (SampleCollection) context.getBean("sampleCollection");
             
            System.out.println("************List Items************");
            System.out.println(sampleCollection.getList());
            System.out.println();
           
            System.out.println("*************SetItems**************");
            System.out.println(sampleCollection.getSet());
            System.out.println();
           
            System.out.println("************Map Items**************");
            System.out.println(sampleCollection.getMap());
            System.out.println();
           
            System.out.println("********PropertiesItems*************");
            System.out.println(sampleCollection.getProperties());
            System.out.println();
      }

}

Required Libraries:

Ø      org.springframework.core-3.0.5.RELEASE.jar
Ø      org.springframework.context-3.0.5.RELEASE.jar
Ø      org.springframework.beans-3.0.5.RELEASE.jar
Ø      org.springframework.asm-3.0.5.RELEASE.jar
Ø      org.apache.log4j-1.2.15.jar
Ø      org.apache.commons.logging-1.1.1.jar
Ø      org.springframework.expression-3.0.5.RELEASE.jar

Thank you please leave you comment. :)

1 comment:

UA-41474183-1