Saturday 8 June 2013

Spring Collection injection or Wiring



Collection Wiring in Spring


Spring Framework provides the support for the Java Collections. It provides support for list, set, map and props java elements. We can use java collection elements by passing direct values of the collection and also by passing a reference of a bean as one of the collection elements.

The Supported Java collection elements are as below:-


<list>:-> It helps in injecting a list of values, as we know list allows duplicates.
<set>:-> It helps in injecting a set of values and as we know set does not allow duplicates.
<map>:->It can be used to inject a bunch of key-value pairs where key and value can be of any object.
<props>:- > It can be used to inject a bunch of key-value pairs where the key and value are Strings.




  List Collection Wiring in Spring 

Example of Spring List property or List Collection injection in spring.


System Requirements:-


         Eclipse Editor or any other.
         JDK 1.5 or higher(I am using jdk 1.7.0_03)
         Spring jars.

Required Jars are:-

spring-core.jar
spring-2.5.jar
spring-beans.jar
spring-context.jar
commons-logging.jar
dom4j-1.4.jar
antlr.jar
log4j.jar

Steps for creating Eclipse java project for implementing List injection or wiring:-

Create a java project in eclipse.
Create a package in the src folder with the name as com.spring.gaurav.collection.list.use.example.
Create the below files in this package and place the corresponding code in those files.
Create an XML file and name it as collection-list-use-in-spring-framework.xml and place this file in the classpath outside the src folder.
Execute the ListCollectionUseInSpringFrameworkClient.java by selecting the option Run as Java Application.

 
SaarcNations.java

packagecom.spring.gaurav.collection.list.use.example;

public classSaarcNations {
            privateString nationName;
            privateString nationCapital;

            publicString getNationName() {
                        returnnationName;
            }

            publicvoid setNationName(String nationName) {
                        this.nationName = nationName;
            }

            publicString getNationCapital() {
                        returnnationCapital;
            }

            publicvoid setNationCapital(String nationCapital) {
                        this.nationCapital = nationCapital;
            }

}


AsianCountries.java






packagecom.spring.gaurav.collection.list.use.example;

import java.util.List;

public classAsianCountries {

            privateList<SaarcNations> nationList;

            publicList<SaarcNations> getNationList() {
                        returnnationList;
            }

            publicvoid setNationList(List<SaarcNations> nationList) {
                        this.nationList = nationList;
            }

}





collection-list-use-in-spring-framework.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-3.0.xsd">

            <bean id="asianCountriesBeanId"
                        class="com.spring.gaurav.collection.list.use.example.AsianCountries">
                        <property name="nationList">
                                    <!-- We can add values in the list in the below two formats, first by
                                                directly adding the bean or second by adding the referenced bean -->
                                    <list>

                                                <bean class="com.spring.gaurav.collection.list.use.example.SaarcNations">

                                                            <property name="nationName"

                                                                        value="INDIA(REPUBLIC OF INDIA (BHARAT GANRAJYA))" />

                                                            <property name="nationCapital" value="NEW DELHI" />

                                                </bean>
                                                <ref bean="saarcNationBeanId1" />
                                                <ref bean="saarcNationBeanId2" />
                                                <ref bean="saarcNationBeanId3" />
                                                <ref bean="saarcNationBeanId4" />
                                                <ref bean="saarcNationBeanId5" />
                                                <ref bean="saarcNationBeanId6" />
                                                <ref bean="saarcNationBeanId7" />
                                    </list>

                        </property>
            </bean>
            <bean id="saarcNationBeanId1"
                        class="com.spring.gaurav.collection.list.use.example.SaarcNations">
                        <property name="nationName"
                                    value="SRI LANKA(DEMOCRATIC SOCIALIST REPUBLIC OF SRI LANKA)" />
                        <property name="nationCapital" value="COLOMBO" />
            </bean>
            <bean id="saarcNationBeanId2"
                        class="com.spring.gaurav.collection.list.use.example.SaarcNations">
                        <property name="nationName" value="BHUTAN(KINGDOM OF BHUTAN)" />
                        <property name="nationCapital" value="THIMPHU" />
            </bean>
            <bean id="saarcNationBeanId3"
                        class="com.spring.gaurav.collection.list.use.example.SaarcNations">
                        <property name="nationName" value="MALDIVES(REPUBLIC OF THE MALDIVES)" />
                        <property name="nationCapital" value="MALE" />
            </bean>
            <bean id="saarcNationBeanId4"
                        class="com.spring.gaurav.collection.list.use.example.SaarcNations">
                        <property name="nationName" value="NEPAL(DEMOCRATIC REPUBLIC OF NEPAL)" />
                        <property name="nationCapital" value="KATHMANDU" />
            </bean>
            <bean id="saarcNationBeanId5"
                        class="com.spring.gaurav.collection.list.use.example.SaarcNations">
                        <property name="nationName" value="PAKISTAN(ISLAMIC REPUBLIC OF PAKISTAN)" />
                        <property name="nationCapital" value="ISLAMABAD" />
            </bean>
            <bean id="saarcNationBeanId6"
                        class="com.spring.gaurav.collection.list.use.example.SaarcNations">
                        <property name="nationName" value="BANGLADESH(PEOPLE'S REPUBLIC OF BANGLADESH)" />
                        <property name="nationCapital" value="DHAKA" />
            </bean>
            <bean id="saarcNationBeanId7"
                        class="com.spring.gaurav.collection.list.use.example.SaarcNations">
                        <property name="nationName" value="AFGHANISTAN(ISLAMIC REPUBLIC OF AFGHANISTAN)" />
                        <property name="nationCapital" value="KABUL" />
            </bean>
</beans>


Note:- As I mentioned earlier We can use java collection elements by passing direct values of the collection and also by passing a reference of a bean as one of the collection elements. So same we can find in the above bean file.

ListCollectionUseInSpringFrameworkClient.java

packagecom.spring.gaurav.collection.list.use.example;

import java.util.List;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;

public classListCollectionUseInSpringFrameworkClient {
            public static void main(String args[]) {
                        ApplicationContext context = newClassPathXmlApplicationContext(
                                                "collection-list-use-in-spring-framework.xml");
                        AsianCountries asianCountries = (AsianCountries) context
                                                .getBean("asianCountriesBeanId");

                        List<SaarcNations> lst = asianCountries.getNationList();
                        System.out
                                                .println("\nSAARC(SOUTH ASIAN ASSOCIATION FOR REGIONAL COOPERATION) NATIONS AND THEIR CAPITALS ");
                        System.out.println("      ");
                        for (SaarcNations saarcNations : lst) {

                                    System.out.println("\nNATION NAME : - "
                                                            + saarcNations.getNationName() + ", \nNATION CAPITAL : - "
                                                            + saarcNations.getNationCapital());
                        }

            }
}


Result:-

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.

SAARC(SOUTH ASIAN ASSOCIATION FOR REGIONAL COOPERATION) NATIONS AND THEIR CAPITALS
     
NATION NAME : - INDIA(REPUBLIC OF INDIA (BHARAT GANRAJYA)),
NATION CAPITAL : - NEW DELHI

NATION NAME : - SRI LANKA(DEMOCRATIC SOCIALIST REPUBLIC OF SRI LANKA),
NATION CAPITAL : - COLOMBO

NATION NAME : - BHUTAN(KINGDOM OF BHUTAN),
NATION CAPITAL : - THIMPHU

NATION NAME : - MALDIVES(REPUBLIC OF THE MALDIVES),
NATION CAPITAL : - MALE

NATION NAME : - NEPAL(DEMOCRATIC REPUBLIC OF NEPAL),
NATION CAPITAL : - KATHMANDU

NATION NAME : - PAKISTAN(ISLAMIC REPUBLIC OF PAKISTAN),
NATION CAPITAL : - ISLAMABAD

NATION NAME : - BANGLADESH(PEOPLE'S REPUBLIC OF BANGLADESH),
NATION CAPITAL : - DHAKA

NATION NAME : - AFGHANISTAN(ISLAMIC REPUBLIC OF AFGHANISTAN),
NATION CAPITAL : - KABUL

No comments:

Post a Comment