English 中文(简体)
Spring DI - Map Constructor
  • 时间:2024-09-17

Spring DI - Injecting Map Constructor


Previous Page Next Page  

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

The following example shows a class JavaCollection that is using collections as dependency injected using constructor arguments.

Let s update the project created in Spring DI - Create Project chapter. We re adding following files −

    JavaCollection.java − A class containing a collections as dependency.

    MainApp.java − Main apppcation to run and test.

Here is the content of JavaCollection.java file −


package com.tutorialspoint;
import java.util.*;

pubpc class JavaCollection {
   Map<String, String>  addressMap;
   pubpc JavaCollection() {}

   pubpc JavaCollection(Map<String, String> addressMap) {
      this.addressMap = addressMap;
   }
   
   // a setter method to set Map
   pubpc void setAddressMap(Map<String, String> addressMap) {
      this.addressMap = addressMap;
   }

   // prints and returns all the elements of the Map.
   pubpc Map<String, String> getAddressMap() {
      System.out.println("Map Elements :"  + addressMap);
      return addressMap;
   }
}

Following is the content of the MainApp.java file −


package com.tutorialspoint;

import org.springframework.context.ApppcationContext;
import org.springframework.context.support.ClassPathXmlApppcationContext;

pubpc class MainApp {
   pubpc static void main(String[] args) {
      ApppcationContext context = new ClassPathXmlApppcationContext("apppcationcontext.xml");
      JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
      jc.getAddressMap();
   }
}

Following is the configuration file apppcationcontext.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 = "javaCollection" class = "com.tutorialspoint.JavaCollection">
      <constructor-arg name = "addressMap">
         <map>
            <entry key = "1" value = "INDIA"/>
            <entry key = "2" value = "JAPAN"/>
            <entry key = "3" value = "USA"/>
            <entry key = "4" value = "UK"/>
         </map>
      </constructor-arg>
   </bean>
</beans>

Output

Once you are done creating the source and bean configuration files, let us run the apppcation. If everything is fine with your apppcation, it will print the following message −


Map Elements :{1=INDIA, 2=JAPAN, 3=USA, 4=UK}
Advertisements