Spring DI - Map Ref 构造函数

您已经了解了如何使用 Bean 配置文件中的 <property> 标签的 value 属性和对象引用来配置原始数据类型和对象引用。 这两种情况都处理将奇异值传递给 bean。

现在如果你想通过 Map 。 在此示例中,我们展示了使用构造函数注入传递 Map 的直接值。


示例

下面的示例显示了一个类 JavaCollection,它使用集合作为使用构造函数参数注入的依赖项。

让我们更新Spring DI - 创建项目章节中创建的项目。 我们正在添加以下文件 −

  • Address.java − 要用作依赖项的类。

  • JavaCollection.java − 包含依赖项集合的类。

  • MainApp.java − 要运行和测试的主应用程序。

这是 Address.java 文件的内容 −


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 文件的内容 −


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

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 文件的内容 −


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,其中包含所有集合类型的配置 −


<?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>

创建完源和 bean 配置文件后,让我们运行应用程序。 如果您的应用程序一切正常,它将打印以下消息 −

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