Rajinder Menu

Dependency Injection into Map and Properties type properties.


This tutorial shows how to inject values into Map and java.util.Properties type properties of Spring beans.

This example is using following technologies:

1) Spring-framework-3.0.2.RELEASE
2) Eclipse Java EE IDE Helios Release (Eclipse version 3.6)
3) Java SE 6  

Jars Used:
 org.springframework.beans-3.0.2.RELEASE.jar
 org.springframework.core-3.0.2.RELEASE.jar
 org.springframework.context-3.0.2.RELEASE.jar
 org.springframework.asm-3.0.2.RELEASE.jar
 org.springframework.expression-3.0.2.RELEASE.jar
 commons-logging-1.1.1.jar


Step 1: Create a new Project in eclipse.


Create a new project in eclipse by going to File > New > Java Project and name it as SpringDIMap.

Project structure in eclipse is shown below:



There are four files used in this program:

1)      NewsLetterSubscriber.java
2)      NewsLetter.java
3)      MainApp.java
4)      ApplicationContext.java


Step2: Add the source code.


Add the following source code in eclipse.

NewsLetterSubscriber.java
package examples.spring.di;


import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

public class NewsLetterSubscriber {

Map<String,String> subscribersDetailsAsMap;
Map<String,NewsLetter> newsLetterToSend;

Properties subscribersDetailsAsProp;

public void setSubscribersDetailsAsMap(Map<String,String> subscribersDetailsAsMap){
this.subscribersDetailsAsMap=subscribersDetailsAsMap;
}

public Map<String,String> getSubscribersDetailsAsMap(){
return (Map<String,String>)subscribersDetailsAsMap;
}

public void setNewsLetterToSend(Map<String,NewsLetter> newsLetterToSend){
this.newsLetterToSend=newsLetterToSend;
}

public Map<String,NewsLetter> getNewsLetterToSend(){
return (Map<String,NewsLetter>)newsLetterToSend;
}


public void setSubscribersDetailsAsProp(Properties subscribersDetailsAsProp){
this.subscribersDetailsAsProp=subscribersDetailsAsProp;
}

public Properties getSubscribersDetailsAsProp(){
return subscribersDetailsAsProp;
}


public void showSubscriberDetails(){
System.out.println("News Letter Subscribers Details AS MAP:: ");

for(String key : subscribersDetailsAsMap.keySet()){
System.out.println("Name="+key+" Email="+subscribersDetailsAsMap.get(key));
}

System.out.println("**************");

System.out.println("News Letter Subscribers Details AS PROPERTIES:: ");
Iterator iter=subscribersDetailsAsProp.keySet().iterator();
while(iter.hasNext()){
     String key=(String)iter.next();
     System.out.println("Name="+key+" Email="+subscribersDetailsAsProp.getProperty(key));
 }
}

public void showNewsLetterDetails(){

   System.out.println("**************");
   System.out.println("News Letter Details:: ");

   for(String key : newsLetterToSend.keySet()){
   System.out.println("Subject="+key+" News Letter Id="+newsLetterToSend.get(key).getNewsLetterId()+" Author="+newsLetterToSend.get(key).getAuthor());
  }

 }

}


NewsLetter.java
package examples.spring.di;

public class NewsLetter {

String newsLetterId;
String author;

public void setNewsLetterId(String newsLetterId){
this.newsLetterId=newsLetterId;
}

public String getNewsLetterId() {
return newsLetterId;
}


public void setAuthor(String author){
this.author = author;
}

public String getAuthor() {
return author;
}

}


MainApp.java
package examples.spring.di;

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

public class MainApp {
public static void main(String args[]){
ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
NewsLetterSubscriber newsLetter=(NewsLetterSubscriber)ctx.getBean("letterSubscriber");
newsLetter.showSubscriberDetails();
newsLetter.showNewsLetterDetails();
}

}



ApplicationContext.java
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="nLetter1" class="examples.spring.di.NewsLetter">
  <property name="newsLetterId" value="letter1"/>
  <property name="author" value="MR. A"/>
</bean>

<bean id="nLetter2" class="examples.spring.di.NewsLetter">
  <property name="newsLetterId" value="letter2"/>
  <property name="author" value="MR. B"/>
</bean>

<bean id="nLetter3" class="examples.spring.di.NewsLetter">
  <property name="newsLetterId" value="letter2"/>
  <property name="author" value="MR. X"/>
</bean>

<bean id="nLetter4" class="examples.spring.di.NewsLetter">
  <property name="newsLetterId" value="letter2"/>
  <property name="author" value="MR. Y"/>

</bean>


<bean id="letterSubscriber" class="examples.spring.di.NewsLetterSubscriber">

<!-- Injecting 'primitive values' in MAP type property-->
<property name="subscribersDetailsAsMap">
<map>
<entry key="John" value="John@dash.com"/>
<entry key="Sandy" value="Sandy@dash.com"/>
<entry key="Sandeep" value="Sandeep@dash.com"/>
<entry key="Joseph" value="Joseph@dash.com"/>
</map>
</property>

<!-- Injecting 'reference to other beans' in MAP type property-->
<property name="newsLetterToSend">
<map>
<!-- Note 'value-ref' instead of 'value'-->
<entry key="Core Java" value-ref="nLetter1"/>
<entry key="Spring" value-ref="nLetter2"/>
<entry key="Hibernate" value-ref="nLetter3"/>
<entry key="JEE" value-ref="nLetter4"/>
</map>
</property>



<!-- Injecting Dependencies in 'Properties' type property-->
<property name="subscribersDetailsAsProp">
<props>
<prop key="John">John@dash.com</prop>
<prop key="Sandy">Sandy@dash.com</prop>
<prop key="Sandeep">Sandeep@dash.com"</prop>
<prop key="Joseph">Joseph@dash.com"</prop>
</props>
</property>

</bean>

</beans>


Step3: Add jars
Add the above mentioned jars in your project by right clicking on your project SpringDIMap,  go to Build Path > Configure Build Path > Add External JARs....

You can download commons-logging-1.1.1.jar from here. Spring related jars can be found in dist folder of your downloaded Spring 3.0.2 distribution folder.

Step 4: Running the program.

Right Click on the project SpringDIMap and go to Run As > Java Application.

You will see the following output  in the Console Pane.

Output:
News Letter Subscribers Details AS MAP::
Name=John    Email=John@dash.com
Name=Sandy    Email=Sandy@dash.com
Name=Sandeep    Email=Sandeep@dash.com
Name=Joseph    Email=Joseph@dash.com
**************
News Letter Subscribers Details AS PROPERTIES::
Name=John          Email=John@dash.com
Name=Sandeep    Email=Sandeep@dash.com"
Name=Joseph      Email=Joseph@dash.com"
Name=Sandy       Email=Sandy@dash.com
**************
News Letter Details::
Subject=Core Java       News Letter Id=letter1     Author=MR. A
Subject=Spring             News Letter Id=letter2    Author=MR. B
Subject=Hibernate        News Letter Id=letter2    Author=MR. X
Subject=JEE                 News Letter Id=letter2    Author=MR. Y

Thanks!




I would like to know your comments and if you liked the article then please share it on social networking buttons.


No comments:

Post a Comment