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

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

importjava.util.Properties;

public classFruitsProperties {
            privateProperties fruitAndTheirProperties;

            publicProperties getFruitAndTheirProperties() {
                        returnfruitAndTheirProperties;
            }

            publicvoid 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


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

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

importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;

public classPropsCollectionUseInSpringFrameworkClient {
            publicstatic 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