Spring DI - 集合構造器注入



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

現在,如果您想傳遞多個值,例如 Java 集合型別,如 List、Set 和 Properties,該怎麼辦?為了處理這種情況,Spring 提供了以下幾種型別的集合配置元素:

序號 元素和描述
1

<list>

這有助於連線(即注入)一系列值,允許重複。

2

<set>

這有助於連線一系列值,但不允許重複。

3

<props>

這可以用來注入一系列名稱-值對,其中名稱和值都是字串。

您可以使用 <list> 或 <set> 來連線 java.util.Collection 的任何實現或一個**陣列**。

在這個例子中,我們展示了傳遞集合元素的直接值。

示例

下面的示例顯示了一個名為 `JavaCollection` 的類,它使用建構函式引數注入集合作為依賴項。

讓我們更新在Spring DI - 建立專案章節中建立的專案。我們將新增以下檔案:

  • **JavaCollection.java** - 包含集合作為依賴項的類。

  • **MainApp.java** - 用於執行和測試的主要應用程式。

以下是 **JavaCollection.java** 檔案的內容:

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

public class JavaCollection {
   List<String> addressList;
   Set<String>  addressSet;
   Properties addressProp;

   public JavaCollection() {}

   public JavaCollection(List<String> addressList, Set<String> addressSet, 
      Properties addressProp) {
      this.addressList = addressList;
      this.addressSet = addressSet;
      this.addressProp = addressProp;
   }
   // a setter method to set List
   public void setAddressList(List<String> addressList) {
      this.addressList = addressList;
   }
   
   // prints and returns all the elements of the list.
   public List<String> getAddressList() {
      System.out.println("List Elements :"  + addressList);
      return addressList;
   }

   // a setter method to set Set
   public void setAddressSet(Set<String> addressSet) {
      this.addressSet = addressSet;
   }

   // prints and returns all the elements of the Set.
   public Set<String> getAddressSet() {
      System.out.println("Set Elements :"  + addressSet);
      return addressSet;
   }

   // a setter method to set Property
   public void setAddressProp(Properties addressProp) {
      this.addressProp = addressProp;
   }

   // prints and returns all the elements of the Property.
   public Properties getAddressProp() {
      System.out.println("Property Elements :"  + addressProp);
      return addressProp;
   }
}

以下是 **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.getAddressList();
      jc.getAddressSet();
      jc.getAddressProp();
   }
}

以下是包含所有型別集合配置的配置檔案 **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 = "javaCollection" class = "com.tutorialspoint.JavaCollection">
      <constructor-arg name = "addressList">
         <list>
            <value>INDIA</value>
            <value>JAPAN</value>
            <value>USA</value>
            <value>UK</value>
         </list>
      </constructor-arg>
      <constructor-arg name = "addressSet">
         <set>
            <value>INDIA</value>
            <value>JAPAN</value>
            <value>USA</value>
            <value>UK</value>
         </set>
      </constructor-arg>
      <constructor-arg name = "addressProp">
         <props>
            <prop key = "one">INDIA</prop>
            <prop key = "two">JAPAN</prop>
            <prop key = "three">USA</prop>
            <prop key = "four">UK</prop>
         </props>
      </constructor-arg>
   </bean>
</beans>

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

List Elements :[INDIA, JAPAN, USA, UK]
Set Elements :[INDIA, JAPAN, USA, UK]
Property Elements :{four=UK, one=INDIA, two=JAPAN, three=USA} 
廣告