Get all updates of Java4s
Follow Us: Email Newsletter

Spring Dependency Injection With Map Collection Property

Spring » On Aug 9, 2011 { 5 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

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..

  • spconfig.xml


Directory structure:



  •  First take our, 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
  • In 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 Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


5 Responses to “Spring Dependency Injection With Map Collection Property”
  1. Hi Sir,

    I think Entry is not a class …it is an static interface inside Map interface…

  2. Java4s says:


    Its class only, you can check this for your reference.

  3. Gautam says:

    dude we can iterate over map like this:

    for(String key : map.keySet()) {
    System.out.println(key+” “+map.get(key));

  4. Arpit says:

    Hi Sivteja,

    Thanks for the simplest tutorial on spring.
    I think from JDK1.7 Map.Entry is interface. Please have a look

  5. Shahbaaz Khokhar says:

    Exception in thread “main” java.lang.NullPointerException showing error for me on iterator snipper in Bean


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.