- Spring DI - Home
- Spring DI - Overview
- Spring DI - Environment Setup
- Spring DI - Create Project
- Spring DI - IOC Containers
- Spring Dependency Injection
Constructor Based Injection Examples
- Spring DI - Constructor Based
- Spring DI - Inner Beans via Constructor
- Spring DI - Collections via Constructor
- Spring DI - Collection Ref via Constructor
- Spring DI - Map via Constructor
- Spring DI - Map Ref via Constructor
Setter Based Injection Examples
- Spring DI - Setter Based
- Spring DI - Inner Beans Setter
- Spring DI - Collections Setter
- Spring DI - Collection Ref Setter
- Spring DI - Map Setter
- Spring DI - Map Ref Setter
Autowiring Examples
- Spring DI - Autowiring
- Spring DI - Autowiring ByName
- Spring DI - Autowiring ByType
- Spring DI - Autowiring Constructor
Factory Method
Spring DI Useful Resources
Spring DI - Injecting Map Reference via Constructor
You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.
Now what if you want to pass Map. In this example, we're showcasing passing direct values of the Map using constructor injection.
Example - Usage of Constructor Based DI of Map Refs
Let's update the project created in Spring DI - Create Project chapter. We're adding following files −
Address.java − A class to be used as dependency.
JavaCollection.java − A class containing a collections of dependencies.
MainApp.java − Main application to run and test.
Address.java
Here is the content of Address.java file −
package com.tutorialspoint;
public class Address {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
JavaCollection.java
Here is the content of JavaCollection.java file −
package com.tutorialspoint;
import java.util.Map;
public class JavaCollection {
Map<String, Address> addressMap;
public JavaCollection() {}
public JavaCollection(Map<String, Address> addressMap) {
this.addressMap = addressMap;
}
// a setter method to set Map
public void setAddressMap(Map<String, Address> addressMap) {
this.addressMap = addressMap;
}
// prints and returns all the elements of the Map.
public Map<String, Address> getAddressMap() {
System.out.println("Map Elements :" + addressMap);
return addressMap;
}
}
MainApp.java
Following is the content of the MainApp.java file −
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressMap();
}
}
applicationcontext.xml
Following is the configuration file applicationcontext.xml which has configuration for all the type of collections −
<?xml version = "1.0" encoding = "UTF-8"?>
<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-3.0.xsd">
<bean id = "address1" class = "com.tutorialspoint.Address">
<property name="name" value="INDIA"></property>
</bean>
<bean id = "address2" class = "com.tutorialspoint.Address">
<property name="name" value="JAPAN"></property>
</bean>
<bean id = "address3" class = "com.tutorialspoint.Address">
<property name="name" value="USA"></property>
</bean>
<bean id = "address4" class = "com.tutorialspoint.Address">
<property name="name" value="UK"></property>
</bean>
<bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
<constructor-arg name = "addressMap">
<map>
<entry key = "1" value-ref = "address1"/>
<entry key = "2" value-ref = "address2"/>
<entry key = "3" value-ref = "address3"/>
<entry key = "4" value-ref = "address4"/>
</map>
</constructor-arg>
</bean>
</beans>
Output
Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message −
Map Elements :{1=INDIA, 2=JAPAN, 3=USA, 4=UK}