Saturday 8 June 2013

How to use Property Collection Injection or Wiring in Spring?



Property 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 Props property or Property Collection injection in spring.



FruitsProperties.java

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

import java.util.Properties;

public class FruitsProperties {
            private Properties fruitAndTheirProperties;

            public Properties getFruitAndTheirProperties() {
                        return fruitAndTheirProperties;
            }

            public void setFruitAndTheirProperties(Properties fruitAndTheirProperties) {
                        this.fruitAndTheirProperties = fruitAndTheirProperties;
            }

}

collection-props-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="fruitsPropertiesBeanId"
                        class="com.spring.gaurav.collection.props.use.example.FruitsProperties">
                        <property name="fruitAndTheirProperties">
                                    <props>
                                                <prop key="WATERMELON">HYPERTENSION</prop>
                                                <prop key="LEMON">KIDNEY STONES</prop>
                                                <prop key="PAPAYA">SKIN ULCERS</prop>
                                                <prop key="KIWIFRUIT ">CHOLESTEROL</prop>
                                                <prop key="COCONUT">GASTRIC ULCERS</prop>
                                                <prop key="STRAWBERRIES">HEART DISEASE</prop>
                                                <prop key="PINEAPPLE">CANCER</prop>
                                                <prop key="GRAPES">INFECTION</prop>
                                    </props>
                        </property>
            </bean>
</beans>


PropsCollectionUseInSpringFrameworkClient.java


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

import java.util.Properties;
import java.util.Set;

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

public class PropsCollectionUseInSpringFrameworkClient {
            public static void main(String args[]) {

                        ApplicationContext context = new ClassPathXmlApplicationContext(
                                                "collection-props-use-in-spring-framework.xml");

                        FruitsProperties fruitsProperties = (FruitsProperties) context
                                                .getBean("fruitsPropertiesBeanId");
                        Properties prop = fruitsProperties.getFruitAndTheirProperties();
                        Set<Object> set = prop.keySet();
                        for (Object obj : set) {
                                    System.out.println("FRUIT NAME :-> " + (String) obj
                                                            + " || HELPS TO REDUCE :->"
                                                            + prop.getProperty((String) obj));
                        }

            }
}

Result:-

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
FRUIT NAME :-> STRAWBERRIES || HELPS TO REDUCE :->HEART DISEASE
FRUIT NAME :-> PAPAYA || HELPS TO REDUCE :->SKIN ULCERS
FRUIT NAME :-> GRAPES || HELPS TO REDUCE :->INFECTION
FRUIT NAME :-> COCONUT || HELPS TO REDUCE :->GASTRIC ULCERS
FRUIT NAME :-> PINEAPPLE || HELPS TO REDUCE :->CANCER
FRUIT NAME :-> WATERMELON || HELPS TO REDUCE :->HYPERTENSION
FRUIT NAME :-> KIWIFRUIT  || HELPS TO REDUCE :->CHOLESTEROL
FRUIT NAME :-> LEMON || HELPS TO REDUCE :->KIDNEY STONES

Back to Interview Questions

No comments:

Post a Comment