logo

Spring Setter Injection With Map


Show

We can achieve the dependency injection in the Spring Setter injection method with the help of Map. In the context of Spring setter dependency injection, a map is used to contain the values that are based on keys such as key and value pair. Each pair of values is known as an entry to the key. Here a map includes distinctive elements only.

In the map parameterized-based dependency injection, an interface is used to represent a mapping between a key and a value. Here, the Map interface is not a subtype of the Collection interface, but it acts as a distinctive type from the other collection types. In the Spring framework of Java, we only use the Map.Entry class object.

Here, in this example, we are using the map as the answer for a question that has an answer as the key and username as the value. We will also use key and value pairs both as a string. This method will use the provided information as the map data.

As we show in many of our previous examples, we will use the example of a forum where one question can have multiple answers.

So let’s better understand this concept with the help of a program;

In this example, we are creating three classes with their brief description.

Question.Java

The objective of the question class is used to include three properties that are id, name, and answers, with its getters & setters and a displayInfo() method to print the output information.

package com.intellinuts;  
import java.util.Iterator;  
import java.util.Map;  
import java.util.Set;  
import java.util.Map.Entry;  

public class Question {  
private int id;  
private String name;  
private Map<String,String> answers;  

//getters and setters  

public void displayInfo(){  
    System.out.println("question id:"+id);  
    System.out.println("question name:"+name);  
    System.out.println("Answers....");  
    Set<Entry<String, String>> set=answers.entrySet();  
    Iterator<Entry<String, String>> itr=set.iterator();  
    while(itr.hasNext()){  
        Entry<String,String> entry=itr.next();  
        System.out.println("Answer:"+entry.getKey()+" Posted By:"+entry.getValue());  
    }  
}  
}  

In the above code, we are using Map.Entry object so that we can print key and values separately by calling getKey() and getValues() methods in the Map. We are using a set to get the iterator from printing the data from the Map. Additionally, we can also use the list element to get the iterator. We are using one method called entrySet() which always returns a Set object.

ApplicationContext.xml

It is a configuration class that needs to be created that includes a Map initializing code. In this file, the entry attribute of the map is used to define the key and value information.

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="q" class="com.intellinuts.Question">  
<property name="id" value="1"></property>  
<property name="name" value="What is Java?"></property>  
<property name="answers">  
<map>  
<entry key="Java is a programming language" value="Sonoo Jaiswal"></entry>  
<entry key="Java is a Platform" value="Sachin Yadav"></entry>  
</map>  
</property>  
</bean>  

</beans>

Now in the above code, we configured some values for the Map property so that the setter method of spring IOC will inject all these values into the Map data property.

Test.Java

Now, to run the program, we will create the test class which is used to get the bean information from the XML configuration file (applicationContext.xml) and invokes the displayInfo() method to print the output result.

package com.intellinuts;  

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 Test {  
public static void main(String[] args) {  
    Resource r=new ClassPathResource("applicationContext.xml");  
    BeanFactory factory=new XmlBeanFactory(r);  

    Question q=(Question)factory.getBean("q");  
    q.displayInfo();  

}  

}  

The output will be printed in the following form:

question id: 1
question name: What is java?
Answers.
Answer: Java is a programming language Posted by: Sonoo Jaiswal
Answer: Java is a Platform Posted by: Sachin Yadav