Spring Inject Map with Example

-
  • In our Spring Application any Spring Bean contains Map type properties,Then Spring Container will use The <map> tag to inject the Map to Spring Bean.
  • To do This in applicationContext.xml under the <property> tag we need to use <map> tag and under the props we have to use <entry> and <key> attribute and <value> tag.

Tools Used In This Article

  • Spring 3.2.16
  • Maven 4.0.0
  • JDK 1.7

The Project Structure


Maven Dependencies

Add The Following Dependencies to the pom.xml

  • spring-core
  • spring-context

<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>SpringMapInjectionDemo</groupId>
<artifactId>SpringMapInjectionDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.16.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.16.RELEASE</version>
</dependency>
</dependencies>
</project>

Hospital.java


package com.testpractice;

import java.util.Map;

public class Hospital {
private Map hospitalNames;

public Map getHospitalNames() {
return hospitalNames;
}

public void setHospitalNames(Map hospitalNames) {
this.hospitalNames = hospitalNames;
}

public void showAccountList() {
System.out.println(hospitalNames);

}


}



applicationContext.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">
<bean id = "hospital" class ="com.testpractice.Hospital">

<property name="hospitalNames">
<map>
<entry key = "1" value = "A"/>
<entry key = "2" value = "B"/>
<entry key = "3" value = "C"/>
<entry key = "4" value = "D"/>
</map>
</property>
</bean>

</beans>

Test.java


package com.testpractice;


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


public class Test {

/**
* @param args
*/
public static void main(String[] args) {

// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
Object object =context.getBean("hospital");
Hospital account = (Hospital)object;

account.showAccountList();


}


}


output


Nov 25, 2016 10:21:43 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4519ab42: defining beans [hospital]; root of factory hierarchy
{1=A, 2=B, 3=C, 4=D}