Constructor Dependency Injection with Map of Custom type Example

In some scenarios, we may need to inject a map of object or map of non string object to another object.
In such cases, we inject a map of object through a constructor.
however this kind of requirements comes very rarely in real time.

Consider the example where Person has an answer sheet which includes Question and Answer.

In this case, let us use Map which stores Question type as a key and Answer type as a value.

Let us see the same in spring through Constructor Dependency Injection

Create a class called Person
It contains 3 attributes id,name and answerSheet where answerSheet is a Map of object.

It also contains showAnswerSheet () method to display the question and answers injected to the person object.

  1. package com.kb.di;
  2.  
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5.  
  6. public class Person {
  7.     private int id;
  8.     private String name;
  9.     private Map<Question, Answer> answerSheet;
  10.  
  11.  
  12.  
  13.     public Person(int id, String name, Map<Question, Answer> answerSheet) {
  14.         super();
  15.         this.id = id;
  16.         this.name = name;
  17.         this.answerSheet = answerSheet;
  18.     }
  19.  
  20.  
  21.  
  22.     public void showAnswerSheet(){
  23.     for (Entry<Question, Answer> entry : answerSheet.entrySet()) {
  24.         System.out.println(entry.getKey().getName()+"-->"+entry.getValue().getName());
  25.     }
  26.        
  27.     }
  28.    
  29. }
package com.kb.di;

import java.util.Map;
import java.util.Map.Entry;

public class Person {
	private int id;
	private String name;
	private Map<Question, Answer> answerSheet;



	public Person(int id, String name, Map<Question, Answer> answerSheet) {
		super();
		this.id = id;
		this.name = name;
		this.answerSheet = answerSheet;
	}



	public void showAnswerSheet(){
	for (Entry<Question, Answer> entry : answerSheet.entrySet()) {
		System.out.println(entry.getKey().getName()+"-->"+entry.getValue().getName());
	}
		
	}
	
}

Create a class called Question

  1. package com.kb.di;
  2.  
  3. public class Question {
  4.     int id;
  5.     String name;
  6.     public Question(int id, String name) {
  7.         super();
  8.         this.id = id;
  9.         this.name = name;
  10.     }
  11.     public int getId() {
  12.         return id;
  13.     }
  14.     public void setId(int id) {
  15.         this.id = id;
  16.     }
  17.     public String getName() {
  18.         return name;
  19.     }
  20.     public void setName(String name) {
  21.         this.name = name;
  22.     }
  23.  
  24. }
package com.kb.di;

public class Question {
	int id;
	String name;
	public Question(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

}

Create a class called Answer

  1. package com.kb.di;
  2.  
  3. public class Answer {
  4.     int id;
  5.     String name;
  6.     public Answer(int id, String name) {
  7.         super();
  8.         this.id = id;
  9.         this.name = name;
  10.     }
  11.     public int getId() {
  12.         return id;
  13.     }
  14.     public void setId(int id) {
  15.         this.id = id;
  16.     }
  17.     public String getName() {
  18.         return name;
  19.     }
  20.     public void setName(String name) {
  21.         this.name = name;
  22.     }
  23.  
  24. }
package com.kb.di;

public class Answer {
	int id;
	String name;
	public Answer(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

}

Create a spring bean definition file

In this file, we used < constructor-arg > tag with < map > to define the answerSheets

< entry > tag is used to inject Map’s key and value.

< key-ref > is used to reference the key defined as a bean
< value-ref > is used to reference the value defined as a bean

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:p="http://www.springframework.org/schema/p"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
  7.  
  8.  <bean id="person" class="com.kb.di.Person">  
  9.     <constructor-arg value="1" type="int"/>
  10.     <constructor-arg value="Raj"/>
  11.     <constructor-arg>
  12.     <map>
  13.     <entry key-ref ="question1" value-ref="answer1"/>
  14.     <entry key-ref="question2" value-ref="answer2"/>
  15.     <entry key-ref="question3" value-ref="answer3"/>
  16.     </map>
  17.     </constructor-arg>
  18.  </bean>
  19.  
  20.  <bean id="question1" class="com.kb.di.Question">  
  21.     <constructor-arg value="1" type="int"/>
  22.     <constructor-arg value="what is java"/>
  23. </bean>
  24.  
  25. <bean id="question2" class="com.kb.di.Question">  
  26.     <constructor-arg value="2" type="int"/>
  27.     <constructor-arg value="what is c++"/>
  28. </bean>
  29. <bean id="question3" class="com.kb.di.Question">  
  30.     <constructor-arg value="3" type="int"/>
  31.     <constructor-arg value="what is c"/>
  32. </bean>
  33.  
  34.  <bean id="answer1" class="com.kb.di.Answer">  
  35.     <constructor-arg value="1" type="int"/>
  36.     <constructor-arg value="java is object oriented langauge"/>
  37. </bean>
  38.  
  39.  <bean id="answer2" class="com.kb.di.Answer">  
  40.     <constructor-arg value="2" type="int"/>
  41.     <constructor-arg value="c++ is also object oriented langauge"/>
  42. </bean>
  43.  
  44.  <bean id="answer3" class="com.kb.di.Answer">  
  45.     <constructor-arg value="3" type="int"/>
  46.     <constructor-arg value="c is procedural oriented language"/>
  47. </bean>
  48.  
  49. </beans>
<?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.2.xsd">
  
 <bean id="person" class="com.kb.di.Person">  
	<constructor-arg value="1" type="int"/>
	<constructor-arg value="Raj"/> 
	<constructor-arg>
	<map>
	<entry key-ref ="question1" value-ref="answer1"/>
	<entry key-ref="question2" value-ref="answer2"/>
	<entry key-ref="question3" value-ref="answer3"/>
	</map>
	</constructor-arg> 
 </bean>
 
 <bean id="question1" class="com.kb.di.Question">  
	<constructor-arg value="1" type="int"/>
	<constructor-arg value="what is java"/> 
</bean>

<bean id="question2" class="com.kb.di.Question">  
	<constructor-arg value="2" type="int"/>
	<constructor-arg value="what is c++"/> 
</bean>
<bean id="question3" class="com.kb.di.Question">  
	<constructor-arg value="3" type="int"/>
	<constructor-arg value="what is c"/> 
</bean>

 <bean id="answer1" class="com.kb.di.Answer">  
	<constructor-arg value="1" type="int"/>
	<constructor-arg value="java is object oriented langauge"/> 
</bean>

 <bean id="answer2" class="com.kb.di.Answer">  
	<constructor-arg value="2" type="int"/>
	<constructor-arg value="c++ is also object oriented langauge"/> 
</bean>

 <bean id="answer3" class="com.kb.di.Answer">  
	<constructor-arg value="3" type="int"/>
	<constructor-arg value="c is procedural oriented language"/> 
</bean>
 
</beans>

Create a pom.xml file

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.   <modelVersion>4.0.0</modelVersion>
  4.  
  5.   <groupId>Spring</groupId>
  6.   <artifactId>SpringCore</artifactId>
  7.   <version>0.0.1-SNAPSHOT</version>
  8.   <packaging>jar</packaging>
  9.  
  10.   <name>SpringCore</name>
  11.   <url>http://maven.apache.org</url>
  12.  
  13.   <properties>
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15.   </properties>
  16.  
  17.   <dependencies>
  18.   <dependency>
  19.     <groupId>org.springframework</groupId>
  20.     <artifactId>spring-core</artifactId>
  21.     <version>4.2.4.RELEASE</version>
  22. </dependency>
  23.   <dependency>
  24.     <groupId>org.springframework</groupId>
  25.     <artifactId>spring-context</artifactId>
  26.     <version>4.2.4.RELEASE</version>
  27. </dependency>
  28.  
  29.     <dependency>
  30.       <groupId>junit</groupId>
  31.       <artifactId>junit</artifactId>
  32.       <version>3.8.1</version>
  33.       <scope>test</scope>
  34.     </dependency>
  35.   </dependencies>
  36. </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Spring</groupId>
  <artifactId>SpringCore</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringCore</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Create a ConstructorDI class

This class loads the spring beans using application context and calls the person class method to display the Answer Sheet of a person.

  1. package com.kb.di;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class ConstructorDI {
  7.    
  8.     public static void main(String[] args) {
  9.         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
  10.         Person person = (Person)applicationContext.getBean("person");
  11.         person.showAnswerSheet();
  12.    
  13.        
  14.     }
  15.  
  16. }
package com.kb.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConstructorDI {
	
	public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
        Person person = (Person)applicationContext.getBean("person");
        person.showAnswerSheet();
    
        
	}

}

Run the ConstructorDI class and see the output as below

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on