In any application, there is a common requirement to use the Map. In normal Java applications, we can create the Map object and we can use that map where ever we want. But in spring, it is quite different the dependencies will be created and injected by spring container.

In the previous tutorial, we have discussed Spring Collection Dependency List Example, this tutorial tells you how to define the java.util.Map collection and how to pass the values to Map collection through DI spring.

Spring Collection Map Dependency :

In a spring bean, if a dependency is of collection type java.util.Map, then in spring configuration file we need to configure the Map collection with <map> tag.

A Map is a collection of entries, and each entry is a collection of key value pairs. To configure the entries and key values, spring provides an <entry> tag to represent the Map entry. And <entry> tag have two properties key and value [highlight color=”blue”] <entry key=” ” value=” “/> [/highlight].

Here is the example to handle the spring collection Map.

Example for Spring Collection Map Dependency :

Project Structure :

Spring Collection Map DependencySpring Maven Dependency :

[sourcecode language=”xml”]
    <!– Spring –>
    <!– Logging with SLF4J & LogBack –>

Spring Beans:

[sourcecode language=”java”]

package com.onlinetutorialspoint.bean;

public class Employee {
    private int empId;
    private String empName;

    public int getEmpId() {
        return empId;

    public void setEmpId(int empId) {
        this.empId = empId;

    public String getEmpName() {
        return empName;

    public void setEmpName(String empName) {
        this.empName = empName;



[sourcecode language=”java”]

package com.onlinetutorialspoint.bean;

public class Department {
    private int deptId;
    private String deptName;
    public int getDeptId() {
        return deptId;

    public void setDeptId(int deptId) {
        this.deptId = deptId;

    public String getDeptName() {
        return deptName;

    public void setDeptName(String deptName) {
        this.deptName = deptName;



[sourcecode language=”java” highlight=”6″]

package com.onlinetutorialspoint.bean;

import java.util.Map;

public class DepartmentMap {
    private Map<Integer, Employee> deptMap;

    public Map<Integer, Employee> getDeptMap() {
        return deptMap;

    public void setDeptMap(Map<Integer, Employee> deptMap) {
        this.deptMap = deptMap;




[sourcecode language=”xml” highlight=”32-41″]

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:context=""
    <bean id="deptBean" class="com.onlinetutorialspoint.bean.Department">
        <property name="deptId" value="20501" />
        <property name="deptName" value="IT" />
    <bean id="deptBean2" class="com.onlinetutorialspoint.bean.Department">
        <property name="deptId" value="20502" />
        <property name="deptName" value="IT" />
    <bean id="deptBean3" class="com.onlinetutorialspoint.bean.Department">
        <property name="deptId" value="20503" />
        <property name="deptName" value="IT" />

    <bean id="empBean" class="com.onlinetutorialspoint.bean.Employee">
        <property name="empId" value="101" />
        <property name="empName" value="Chandra Shekhar" />
    <bean id="empBean2" class="com.onlinetutorialspoint.bean.Employee">
        <property name="empId" value="102" />
        <property name="empName" value="Robert" />
    <bean id="empBean3" class="com.onlinetutorialspoint.bean.Employee">
        <property name="empId" value="103" />
        <property name="empName" value="John" />

    <bean id="deptMap" class="com.onlinetutorialspoint.bean.DepartmentMap">
        <property name="deptMap">
                <entry key="#{deptBean.deptId}" value-ref="empBean" />
                <entry key="#{deptBean2.deptId}" value-ref="empBean2" />
                <entry key="#{deptBean3.deptId}" value-ref="empBean3" />




On the above configuration to access the deptBean properties, we use the [highlight color=”green”]Spring Expression Language[/highlight] support [highlight color=”blue”](“#{deptBean.deptId}”)[/highlight] .

Run the Application :

[sourcecode language=”java”]

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.springframework.context.ApplicationContext;

import com.onlinetutorialspoint.bean.DepartmentMap;
import com.onlinetutorialspoint.bean.Employee;

public class Main {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("springconfiguration.xml");
        DepartmentMap deptMap = (DepartmentMap) context.getBean("deptMap");
        Map<Integer, Employee> map = deptMap.getDeptMap();
        Set<Integer> keySet = map.keySet();
        Iterator  it = keySet.iterator();
        while (it.hasNext()) {
            Integer key = (Integer);
            System.out.println("DeptId : "+key+" Employees Information");
            Employee emp = map.get(key);
            System.out.println("EmployeeId : "+emp.getEmpId());
            System.out.println("EmployeeName : "+emp.getEmpName());



Output :

DeptId : 20501 Employees Information
EmployeeId : 101
EmployeeName : Chandra Shekhar
DeptId : 20502 Employees Information
EmployeeId : 102
EmployeeName : Robert
DeptId : 20503 Employees Information
EmployeeId : 103
EmployeeName : John


Happy Learning 🙂

Download Example