Spring DI - Map Ref Setter



您已經瞭解瞭如何在 Bean 配置檔案中使用<property>標籤的value屬性配置基本資料型別,以及使用ref屬性配置物件引用。這兩種情況都涉及將單個值傳遞給 Bean。

現在,如果您想傳遞 Map 該怎麼辦?在本例中,我們將展示如何使用 Setter 注入傳遞 Map 的直接值。

示例

以下示例顯示了一個類JavaCollection,它使用集合作為使用 Setter 方法注入的依賴項。

讓我們更新在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">
      <property 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>
      </property>
   </bean>
</beans>

輸出

建立原始檔和 Bean 配置檔案後,讓我們執行應用程式。如果應用程式一切正常,它將列印以下訊息:

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

© . All rights reserved.