Setter Injection with Non-String Map Example in Spring

In this page your can learn example, we are using map as the answer that have Answer and User. Here, we are using key and value pair both as an object. Answer has its own information such as answerId, answer and postedDate, User has its own information such as userId, username, emailId.

previous examples, it is the example of forum where one question can have multiple answers.

This class contains three properties, getters and setters and displayInfo() method to display the information.

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

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(); = id;
	this.answer = answer;
	this.postedDate = postedDate;

public String toString(){
	return "Id:"+id+" Answer:"+answer+" Posted Date:"+postedDate;
package com.javatportal;

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

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


The key-ref and value-ref attributes of entry element is used to define the reference of bean in the map.

<?xml version="1.0" encoding="UTF-8"?>

<bean id="answer1" class="com.javatportal.Answer">
<property name="id" value="1"></property>
<property name="answer" value="Java is a Programming Language"></property>
<property name="postedDate" value="12/02/2014"></property>
<bean id="a/swer2" class="com.javatportal.Answer">
<property name="id" value="2"></property>
<property name="answer" value="Java is a Platform"></property>
<property name="postedDate" value="15/08/2015"></property>

<bean id="user1" class="com.javatportal.User">
<property name="id" value="1"></property>
<property name="name" value="Mithilesh Kushwaha"></property>
<property name="email" value=""></property>
<bean id="user2" class="com.javatportal.User">
<property name="id" value="2"></property>
<property name="name" value="Harish Kumar"></property>
<property name="email" value=""></property>

<bean id="q" class="com.javatportal.Question">
<property name="id" value="1"></property>
<property name="name" value="What is Java?"></property>
<property name="answers">
<entry key-ref="answer1" value-ref="user1"></entry>
<entry key-ref="answer2" value-ref="user2"></entry>


This class gets the bean from the applicationContext.xml file and calls the displayInfo() method to display the information.

package com.javatportal;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

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");