Get all updates of Java4s
Newsletter

Spring Dependency Injection With Map Collection Property

Spring »on Aug 9, 2011 { 6 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.

k1Oracle
k2Sun
k3Java4s
k4id2

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

  • WelcomeBean.java
  • ClientLogic.java
  • spconfig.xml

 

Directory structure:

 

WelcomeBean.java

spconfig.xml

ClientLogic.java

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

Java4s_Author
- Full Stack Java/J2EE & UI Web Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

6 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. Gautam says:

    dude we can iterate over map like this:

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

    • Chandrakanth says:

      hahaaa obviously ;)
      in our situation we use like >>

      for (Object key : data.keySet()) {
      System.out.println("key = "+key + " value = " + data.get(key));
      }

  3. Arpit says:

    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

  4. Shahbaaz Khokhar says:

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

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Spring-Boot-tutorials Hibernate tutorials Spring tutorials Struts tutorials Servlets tutorials Ajax tutorials Log4j tutorials jQuery tutorials Json tutorials Core Java tutorials Web Services tutorials AngularJs tutorials
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Contact | About Us | Privacy Policy | Advertise With Us

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