Newsletter |
Spring Dependency Injection With Map Collection Property
Spring » on Aug 9, 2011 { 7 Comments } By Sivateja
Map will stores the data in key, value base that to key, value must be the objects of any type. One [ key, value ] is called one pair or one entry.
k1 | Oracle |
k2 | Sun |
k3 | Java4s |
k4 | id2 |
See here we have two entries [two pairs], one entry is k1 — 100 and second entry is k2 — 200
- Let us come to the concept, Map.Entry will gives one entry
- Map is the interface, Entry is the static class in Map interface, so we can call Map.Entry just remember this concept as of now
- Actually In this spring we always use Map.Entry class object only
In the XML
<map> <entry key="k1"> <value>Oracle</value> </entry> <entry key="k2"> <value>Sun</value> </entry> <entry key="k3"> <value>Java4s</value> </entry> <entry key="k4" value-ref="id2" /> </map>
See in the xml, we have to configure like this, so we have 3 pairs [ key, values] means spring container will creates 3 Map.Entry class objects and stores those objects in <map />
One <entry /> tag will produces one object of Map.Entry, by the way k4 is to getting other bean class object.
Complete Example Using Map Collection
files will be used..
- WelcomeBean.java
- ClientLogic.java
- spconfig.xml
WelcomeBean.java
package java4s; import java.util.Iterator; import java.util.Map; import java.util.Set; public class WelcomeBean { private Map data; public void setData(Map data) { this.data = data; } public void show() { Set s=data.entrySet(); Iterator it = s.iterator(); while(it.hasNext()) { Map.Entry me = (Map.Entry)it.next(); System.out.println(me.getKey()+ " - "+me.getValue()); } } }
spconfig.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="id1" class="java4s.WelcomeBean"> <property name="data" > <map> <entry key="k1"> <value>10</value> </entry> <entry key="k2"> <value>java4s</value> </entry> <entry key="k3"> <value>10.55</value> </entry> </map> </property> </bean> </beans>
ClientLogic.java
package java4s; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ClientLogic { public static void main(String[] args) { Resource res = new ClassPathResource("spconfig.xml"); BeanFactory factory = new XmlBeanFactory(res); Object o = factory.getBean("id1"); WelcomeBean wb = (WelcomeBean)o; wb.show(); } }
Explanation:
- First take our ClientLogic.java, see line number 15 when we call factory.getBean(“id1”) then spring IOC will go to spconfig.xml and verify the class name having id1, [ you can see this in spconfig.xml line number 5, there our class name is WelcomeBean ]
- Now it will jumps to WelcomeBean.java
- In WelcomeBean.java we have been taken Map property [ see line number 7 ], and written setter property for that Map
- In spconfig.xml we configured some values for that Map property right, so with this setter method spring IOC will inject all these values into Map data property
- now come to show() method at line number 13, here see the concept actually we don’t have any iterator for Map right, hope you know this fact 🙂 , but we need iterator to get and print the data from the Map, moreover List,Set have the iterator.
- So now we need Set or List to get the iterator, so am going to take Set, at line number 15, of course you can go with List as well
- Actually data is of Map type, in Map we have one method called entrySet() which always returns Set object, so i got the iterator by using this Set object at line number 16
- Now see line number 19, i have been typecast into Map.Entry, so that i can print key and values separately by calling getKey() and getValues() methods in the Map.Entry Class [ I already given the concept about this Map.Entry go to top and check if you have any doubt regarding this ]
- And we done 🙂
And mates this Map having much importance in java remember….!!!!!!
You Might Also Like
::. About the Author .:: | ||
Comments
7 Responses to “Spring Dependency Injection With Map Collection Property”
Hi Sir,
I think Entry is not a class …it is an static interface inside Map interface…
@Shailesh
Its class only, you can check this for your reference.
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Map.Entry.html
dude we can iterate over map like this:
for(String key : map.keySet()) {
System.out.println(key+” “+map.get(key));
}
hahaaa obviously 😉
in our situation we use like >>
for (Object key : data.keySet()) {
System.out.println("key = "+key + " value = " + data.get(key));
}
Hi Sivteja,
Thanks for the simplest tutorial on spring.
I think from JDK1.7 Map.Entry is interface. Please have a look
http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
Exception in thread “main” java.lang.NullPointerException showing error for me on iterator snipper in Bean
we can iterate over map like this:
Iterator<Map.Entry> it=studentData.entrySet().iterator();
while(it.hasNext()){
Map.Entry<Integer,String> entries=it.next();
System.out.println("Map "+entries.getKey() +"value" +entries.getValue() );