- Spring DI 教程
- Spring DI - 首頁
- Spring DI - 概述
- Spring DI - 環境搭建
- Spring DI - IOC容器
- Spring 依賴注入
- Spring DI - 建立專案
- 基於構造器的注入示例
- Spring DI - 基於構造器的注入
- Spring DI - 內部Bean構造器注入
- Spring DI - 集合型別構造器注入
- Spring DI - 集合引用構造器注入
- Spring DI - Map 構造器注入
- Spring DI - Map 引用構造器注入
- 基於Setter方法的注入示例
- Spring DI - 基於Setter方法的注入
- Spring DI - 內部Bean Setter注入
- Spring DI - 集合屬性注入
- Spring DI - 集合引用Setter注入
- Spring DI - Map Setter注入
- Spring DI - Map 引用Setter注入
- 自動裝配示例
- Spring DI - 自動裝配
- Spring DI - 按名稱自動裝配
- Spring DI - 按型別自動裝配
- Spring DI - 構造器自動裝配
- 工廠方法
- Spring DI - 靜態工廠方法
- Spring DI - 非靜態工廠方法
- Spring DI 有用資源
- Spring DI - 快速指南
- Spring DI - 有用資源
- Spring DI - 討論
Spring DI - 集合屬性注入
您已經瞭解瞭如何使用value屬性配置基本資料型別,以及使用Bean配置檔案中<property>標籤的ref屬性配置物件引用。這兩種情況都處理的是向Bean傳遞單個值。
現在,如果您想傳遞多個值,例如Java集合型別,如List、Set、Map和Properties,該怎麼辦?為了處理這種情況,Spring提供了以下型別的集合配置元素:
| 序號 | 元素及描述 |
|---|---|
| 1 |
<list> 這有助於連線,即注入一系列值,允許重複。 |
| 2 |
<set> 這有助於連線一系列值,但不允許重複。 |
| 3 |
<props> 這可以用來注入一系列名稱-值對,其中名稱和值都是字串。 |
您可以使用<list>或<set>來連線java.util.Collection的任何實現或一個陣列。
在這個例子中,我們展示了傳遞集合元素的直接值。
示例
下面的示例顯示了一個名為JavaCollection的類,它使用Setter方法注入集合作為依賴項。
讓我們更新在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;
// 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">
<!-- Definition for javaCollection -->
<bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
<!-- results in a setAddressList(java.util.List) call -->
<property name = "addressList">
<list>
<value>INDIA</value>
<value>JAPAN</value>
<value>USA</value>
<value>UK</value>
</list>
</property>
<!-- results in a setAddressSet(java.util.Set) call -->
<property name = "addressSet">
<set>
<value>INDIA</value>
<value>JAPAN</value>
<value>USA</value>
<value>UK</value>
</set>
</property>
<!-- results in a setAddressProp(java.util.Properties) call -->
<property name = "addressProp">
<props>
<prop key = "one">INDIA</prop>
<prop key = "two">JAPAN</prop>
<prop key = "three">USA</prop>
<prop key = "four">UK</prop>
</props>
</property>
</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}
廣告