Spring framework allows to inject Map collection in the setter method while creating bean using property element by specifying the Map element. The Map element must be specify inside the property element.
Steps for setter injection with Map
1. | Create class with Map property |
2. | Create configuration xml |
3. | Create bean with Map collection |
4. | Validate using java application |
Create class with Map property
Create class with Map property
1 package com.iogyan.model;
2
3 import java.util.Map;
4
5 public class Address {
6
7 private Map<String, String> addressLine;
8
9 public void setAddressLine(Map<String, String> addressLine) {
10 this.addressLine = addressLine;
11 }
12
13 public Map<String, String> getAddressLine() {
14 return this.addressLine;
15 }
16 }
In the above example, a class Address is created with class member of type Map. A setter setAddress() method sets the property value and access using getter method that returns map of class property.
Create configuration xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:context="http://www.springframework.org/schema/context"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans.xsd">
6
7 </beans>
In the above configuration, a xml configuration grammar is define with required spring namespaces that we are using to define a spring bean. A beans namespace used to define a bean and context namespace allows to access spring bean using configuration bean factory.
Create bean with Map collection
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:context="http://www.springframework.org/schema/context"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans.xsd">
6
7 <bean id="address" class="com.iogyan.model.Address">
8 <property name="addressLine">
9 <map>
10 <entry key="currentAddress" value="Ahmedabad Gujarat India"></entry>
11 <entry key="permanentAddress" value="Mumbai Maharashtra India"></entry>
12 </map>
13 </property>
14 </bean>
15 </beans>
In the above configuration, an Address class bean is created by specifying a property id and class with full package. A setter dependency is specified with property tag by specifying a map tag nested with entry tag by specifying key and value. It adds two entries in map and inject dependency map object. A value can be specified as nested tag of entry tag.
Validate using java application
Validate using java application
1 package com.iogyan.example;
2
3 import org.springframework.context.support.ClassPathXmlApplicationContext;
4 import com.iogyan.model.Address;
5
6 public class Example {
7
8 public static void main(String[] args) {
9
10
11 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
12 context.setConfigLocation("applicationContext.xml");
13 context.refresh();
14
15 Address address = (Address) context.getBean("address");
16 System.out.println(address.getAddressLine());
17 context.close();
18 }
19 }
In the above example, a ClassPathXmlApplicationContext is created using constructor method and called a setConfigLocation() method by specifying a configuration xml that reads the bean definition and creates bean in spring container and refresh spring bean container. A context method getBean() called by specifying a bean ID address that returns a bean and assigned to the class object. A Address class getAddressLine() method called that prints Map object.
1 {currentAddress=Ahmedabad Gujarat India, permanentAddress=Mumbai Maharashtra India}
Related options for your search