logo

Spring Setter Injection With Non-String Map


Show

In the Spring framework of Java, we can inject the dependency between the objects by using the map of the custom object or non-string object. In this case, we are just required to inject a map of Custom objects into another object.

In this example, we are using a map as the answer that has the properties of answers and users. In the Non-string map, we are using a key as well as a value pair as an object. Here the object is dependent because it depends on the values of answers and users. The answer entity of the Non-string map includes information like answerId, answer, and postedDate. Similarly, the User entity also has its information such as userId, username, emailId.

Like the previous examples where we are using a forum representing one question that can have multiple answers, we do the same here. In this example, we use a map to store the type of question as a key and the type of answer as the value.

So let’s get started to know the concept of dependency injection with the help of a Non-string map.

Question.Java

First of all, create a class called the question. The question class contains three attributes or properties that are id, name, and Answers, where the Answer attribute is a map of objects. This class includes getter and setter as well as displayInfo() method to display the values of the properties injected into the question object.

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<Answer,User> 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<Answer, User>> set=answers.entrySet();  
    Iterator<Entry<Answer, User>> itr=set.iterator();  
    while(itr.hasNext()){  
        Entry<Answer, User> entry=itr.next();  
        Answer ans=entry.getKey();  
        User user=entry.getValue();  
        System.out.println("Answer Information:");  
        System.out.println(ans);  
        System.out.println("Posted By:");  
        System.out.println(user);  
    }  
}  

}  

Answer.Java

We will create the Answer class. This class will specify the type of Answer attributes, such as integer for id, a string for an answer, and date for postedDate values. This class will return the attribute values.

package com.intellinuts;  

import java.util.Date;  

public class Answer {  
private int id;  
private String answer;  
private Date postedDate;  
public Answer() {}  
public Answer(int id, String answer, Date postedDate) {  
    super();  
    this.id = id;  
    this.answer = answer;  
    this.postedDate = postedDate;  
}  

public String toString(){  
    return "Id:"+id+" Answer:"+answer+" Posted Date:"+postedDate;  
}  
}  

User.Java

The class is created to include the details of the user which provides the answer to the question class. It consists of the attribute type of the user information such as id, name, and email. This class will return the values of these attribute types.

package com.intellinuts;  

public class User {  
private int id;  
private String name,email;  
public User() {}  
public User(int id, String name, String email) {  
    super();  
    this.id = id;  
    this.name = name;  
    this.email = email;  
}  

public String toString(){  
    return "Id:"+id+" Name:"+name+" Email Id:"+email;  
}  
}  

ApplicationContext.xml

Now, we will create the XML configuration file, where the key-ref and value-ref attributes of the entry element are used to define the reference of bean in the map. Here, the Entry tag is used to inject the Map’s key and value.

<?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="answer1" class="com.intellinuts.Answer">  
<property name="id" value="1"></property>  
<property name="answer" value="Java is a Programming Language"></property>  
<property name="postedDate" value="12/12/2001"></property>  
</bean>  

<bean id="answer2" class="com.intellinuts.Answer">  
<property name="id" value="2"></property>  
<property name="answer" value="Java is a Platform"></property>  
<property name="postedDate" value="12/12/2003"></property>  
</bean>  

<bean id="user1" class="com.intellinuts.User">  
<property name="id" value="1"></property>  
<property name="name" value="Arun Kumar"></property>  
<property name="email" value="arun@gmail.com"></property>  
</bean>  

<bean id="user2" class="com.intellinuts.User">  
<property name="id" value="2"></property>  
<property name="name" value="Varun Kumar"></property>  
<property name="email" value="Varun@gmail.com"></property>  
</bean>  

<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-ref="answer1" value-ref="user1"></entry>  
<entry key-ref="answer2" value-ref="user2"></entry>  
</map>  
</property>  
</bean>  

</beans>  

Test.Java

This class gets the bean from the applicationContext.xml file and calls the displayInfo() method to display the information such as answer and user details in reply to the question.

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 as follows:

Question id: 1
Question name: What is Java?
Answers...

Answer Information:
Id: 1 Answer: Java is a Programming Language Posted Date: 12/12/2001
Posted By:
Id: 1 Name: Arun Kumar Email: arun@gmail.com

Id: 2 Answer: Java is a Platform Posted Date: 12/12/2003
Posted By:
Id: 2 Name: Varun Kumar Email: Varun@gmail.com