Saturday, 8 June 2013

How to use Set Collection Injection or Wiring in Spring?



 Set Collection Wiring in Spring 


For the System requirements, project required jars and project creation setup steps please refer the below links:-

http://www.javatechtipssharedbygaurav.com/2013/06/collection-injection-in-spring.html

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


SetOfMarkupLanguage.java

package com.spring.gaurav.collection.set.use.example;

public class SetOfMarkupLanguage {
            private String markupLanguageAbbreviations;
            private String markupLanguageDescription;

            public String getMarkupLanguageAbbreviations() {
                        return markupLanguageAbbreviations;
            }

            public void setMarkupLanguageAbbreviations(
                                    String markupLanguageAbbreviations) {
                        this.markupLanguageAbbreviations = markupLanguageAbbreviations;
            }

            public String getMarkupLanguageDescription() {
                        return markupLanguageDescription;
            }

            public void setMarkupLanguageDescription(String markupLanguageDescription) {
                        this.markupLanguageDescription = markupLanguageDescription;
            }

}


MarkupLanguageTypes.java

package com.spring.gaurav.collection.set.use.example;

import java.util.List;
import java.util.Set;

public class MarkupLanguageTypes {
            private List<String> typesOfMarkupLanguage;
            private Set<SetOfMarkupLanguage> markupLanguageSet;

            public List<String> getTypesOfMarkupLanguage() {
                        return typesOfMarkupLanguage;
            }

            public void setTypesOfMarkupLanguage(List<String> typesOfMarkupLanguage) {
                        this.typesOfMarkupLanguage = typesOfMarkupLanguage;
            }

            public Set<SetOfMarkupLanguage> getMarkupLanguageSet() {
                        return markupLanguageSet;
            }

            public void setMarkupLanguageSet(Set<SetOfMarkupLanguage> markupLanguageSet) {
                        this.markupLanguageSet = markupLanguageSet;
            }

}

collection-set-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="markupLanguageTypesBeanId"
                        class="com.spring.gaurav.collection.set.use.example.MarkupLanguageTypes">
                        <property name="typesOfMarkupLanguage">
                                    <list>
                                                <value>PRESENTATIONAL MARKUP</value>
                                                <value>PROCEDURAL MARKUP</value>
                                                <value>DESCRIPTIVE MARKUP</value>
                                    </list>

                        </property>
                        <property name="markupLanguageSet">
                                    <!-- We can add values in the set in the below two formats, first by directly

                                                adding the bean or second by adding the referenced bean -->
                                    <set>
                                                <bean
                                                            class="com.spring.gaurav.collection.set.use.example.SetOfMarkupLanguage">
                                                            <property name="markupLanguageAbbreviations" value="GML" />
                                                            <property name="markupLanguageDescription" value="IBM GENERALIZED MARKUP LANGUAGE" />
                                                </bean>
                                                <ref bean="setOfMarkupLanguageBeanId1" />
                                                <ref bean="setOfMarkupLanguageBeanId2" />
                                                <ref bean="setOfMarkupLanguageBeanId3" />
                                                <ref bean="setOfMarkupLanguageBeanId4" />
                                    </set>

                        </property>
            </bean>
            <bean id="setOfMarkupLanguageBeanId1"
                        class="com.spring.gaurav.collection.set.use.example.SetOfMarkupLanguage">
                        <property name="markupLanguageAbbreviations"
                                    value="SGML" />
                        <property name="markupLanguageDescription" value="STANDARD GENERALIZED MARKUP LANGUAGE" />
            </bean>
            <bean id="setOfMarkupLanguageBeanId2"
                        class="com.spring.gaurav.collection.set.use.example.SetOfMarkupLanguage">
                        <property name="markupLanguageAbbreviations" value="XHTML" />
                        <property name="markupLanguageDescription" value="EXTENSIBLE HYPERTEXT MARKUP LANGUAGE" />
            </bean>
            <bean id="setOfMarkupLanguageBeanId3"
                        class="com.spring.gaurav.collection.set.use.example.SetOfMarkupLanguage">
                        <property name="markupLanguageAbbreviations" value="HTML" />
                        <property name="markupLanguageDescription" value="HYPERTEXT MARKUP LANGUAGE" />
            </bean><bean id="setOfMarkupLanguageBeanId4"
                        class="com.spring.gaurav.collection.set.use.example.SetOfMarkupLanguage">
                        <property name="markupLanguageAbbreviations" value="XML" />
                        <property name="markupLanguageDescription" value="EXTENSIBLE MARKUP LANGUAGE" />
            </bean>
</beans>


SetCollectionUseInSpringFrameworkClient.java

package com.spring.gaurav.collection.set.use.example;

import java.util.List;
import java.util.Set;

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

public class SetCollectionUseInSpringFrameworkClient {
            public static void main(String args[]) {
                        ApplicationContext context = new ClassPathXmlApplicationContext(
                                                "collection-set-use-in-spring-framework.xml");

                        MarkupLanguageTypes markupLanguageTypes = (MarkupLanguageTypes) context
                                                .getBean("markupLanguageTypesBeanId");

                        List<String> strLst = markupLanguageTypes.getTypesOfMarkupLanguage();
                        System.out.println("TYPES OF MARKUP LANGUAGE ");
                        System.out.println("*****************************");
                        for (String str : strLst) {
                                    System.out.println(str);
                        }
                        Set<SetOfMarkupLanguage> set = markupLanguageTypes
                                                .getMarkupLanguageSet();
                        System.out.println("\nMARKUP LANGUAGE EXAMPLES ");
                        System.out.println("*****************************");
                        for (SetOfMarkupLanguage setOfMarkupLanguage : set) {

                                    System.out.println("\nMARKUP LANGUAGE ABBREVIATIONS : - "
                                                            + setOfMarkupLanguage.getMarkupLanguageAbbreviations()
                                                            + ", \nMARKUP LANGUAGE DESCRIPTION : - "
                                                            + setOfMarkupLanguage.getMarkupLanguageDescription());
                        }

            }
}


Result:-

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
TYPES OF MARKUP LANGUAGE
*****************************
PRESENTATIONAL MARKUP
PROCEDURAL MARKUP
DESCRIPTIVE MARKUP

MARKUP LANGUAGE EXAMPLES
*****************************

MARKUP LANGUAGE ABBREVIATIONS : - GML,
MARKUP LANGUAGE DESCRIPTION : - IBM GENERALIZED MARKUP LANGUAGE

MARKUP LANGUAGE ABBREVIATIONS : - SGML,
MARKUP LANGUAGE DESCRIPTION : - STANDARD GENERALIZED MARKUP LANGUAGE

MARKUP LANGUAGE ABBREVIATIONS : - XHTML,
MARKUP LANGUAGE DESCRIPTION : - EXTENSIBLE HYPERTEXT MARKUP LANGUAGE

MARKUP LANGUAGE ABBREVIATIONS : - HTML,
MARKUP LANGUAGE DESCRIPTION : - HYPERTEXT MARKUP LANGUAGE

MARKUP LANGUAGE ABBREVIATIONS : - XML,
MARKUP LANGUAGE DESCRIPTION : - EXTENSIBLE MARKUP LANGUAGE

No comments:

Post a Comment