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 the spring container.
In the previous tutorial, we have discussed the 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 has two properties key and value <entry key=" " value=" "/>
Here is the example to handle the spring collection Map.
Example for Spring Collection Map Dependency :
Project Structure :
Spring Maven Dependency :
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
Spring Beans:
Employee.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;
}
}
Department.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;
}
}
DepartmentMap.java
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;
}
}
springconfiguration.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="deptBean" class="com.onlinetutorialspoint.bean.Department">
<property name="deptId" value="20501" />
<property name="deptName" value="IT" />
</bean>
<bean id="deptBean2" class="com.onlinetutorialspoint.bean.Department">
<property name="deptId" value="20502" />
<property name="deptName" value="IT" />
</bean>
<bean id="deptBean3" class="com.onlinetutorialspoint.bean.Department">
<property name="deptId" value="20503" />
<property name="deptName" value="IT" />
</bean>
<bean id="empBean" class="com.onlinetutorialspoint.bean.Employee">
<property name="empId" value="101" />
<property name="empName" value="Chandra Shekhar" />
</bean>
<bean id="empBean2" class="com.onlinetutorialspoint.bean.Employee">
<property name="empId" value="102" />
<property name="empName" value="Robert" />
</bean>
<bean id="empBean3" class="com.onlinetutorialspoint.bean.Employee">
<property name="empId" value="103" />
<property name="empName" value="John" />
</bean>
<bean id="deptMap" class="com.onlinetutorialspoint.bean.DepartmentMap">
<property name="deptMap">
<map>
<entry key="#{deptBean.deptId}" value-ref="empBean" />
<entry key="#{deptBean2.deptId}" value-ref="empBean2" />
<entry key="#{deptBean3.deptId}" value-ref="empBean3" />
</map>
</property>
</bean>
</beans>
On the above configuration to access the deptBean properties, we use the Spring Expression Language support (“#{deptBean.deptId}”) .
Run the Application :
Main.java
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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) it.next();
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 🙂