- Spring核心基礎
- Spring - 首頁
- Spring - 概述
- Spring - 架構
- Spring - 環境搭建
- Spring - HelloWorld示例
- Spring - IoC容器
- Spring - Bean定義
- Spring - Bean作用域
- Spring - Bean生命週期
- Spring - Bean後處理器
- Spring - Bean定義繼承
- Spring - 依賴注入
- Spring - 注入內部Bean
- Spring - 集合注入
- Spring - Bean自動裝配
- 基於註解的配置
- Spring - 基於Java的配置
- Spring - Spring中的事件處理
- Spring - Spring中的自定義事件
- Spring - Spring框架中的AOP
- Spring - JDBC框架
- Spring - 事務管理
- Spring - Web MVC框架
- Spring - 使用Log4J進行日誌記錄
- Spring問答
- Spring - 問答
- Spring有用資源
- Spring - 快速指南
- Spring - 有用資源
- Spring - 討論
Spring - 集合注入
您已經瞭解瞭如何使用value屬性配置基本資料型別,以及使用<property>標籤的ref屬性配置物件引用。這兩種情況都處理將單個值傳遞給bean。
現在,如果您想傳遞多個值,例如Java集合型別,如List、Set、Map和Properties,該怎麼辦?為了處理這種情況,Spring提供了四種類型的集合配置元素,如下所示:
| 序號 | 元素和描述 |
|---|---|
| 1 |
<list> 這有助於連線即注入值的列表,允許重複。 |
| 2 |
<set> 這有助於連線一組值,但不允許重複。 |
| 3 |
<map> 這可以用來注入名稱-值對的集合,其中名稱和值可以是任何型別。 |
| 4 |
<props> 這可以用來注入名稱-值對的集合,其中名稱和值都是字串。 |
您可以使用<list>或<set>來連線java.util.Collection的任何實現或陣列。
您會遇到兩種情況:(a)傳遞集合的直接值,以及(b)傳遞bean的引用作為集合元素之一。
示例
讓我們準備好一個Eclipse IDE,並按照以下步驟建立一個Spring應用程式:
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為SpringExample的專案,並在建立的專案中的src資料夾下建立一個com.tutorialspoint包。 |
| 2 | 如Spring HelloWorld示例章節中所述,使用新增外部JAR選項新增所需的Spring庫。 |
| 3 | 在com.tutorialspoint包下建立Java類JavaCollection和MainApp。 |
| 4 | 在src資料夾下建立Beans配置檔案Beans.xml。 |
| 5 | 最後一步是建立所有Java檔案和Bean配置檔案的內容,並按如下所述執行應用程式。 |
以下是JavaCollection.java檔案的內容:
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
List addressList;
Set addressSet;
Map addressMap;
Properties addressProp;
// a setter method to set List
public void setAddressList(List addressList) {
this.addressList = addressList;
}
// prints and returns all the elements of the list.
public List getAddressList() {
System.out.println("List Elements :" + addressList);
return addressList;
}
// a setter method to set Set
public void setAddressSet(Set addressSet) {
this.addressSet = addressSet;
}
// prints and returns all the elements of the Set.
public Set getAddressSet() {
System.out.println("Set Elements :" + addressSet);
return addressSet;
}
// a setter method to set Map
public void setAddressMap(Map addressMap) {
this.addressMap = addressMap;
}
// prints and returns all the elements of the Map.
public Map getAddressMap() {
System.out.println("Map Elements :" + addressMap);
return addressMap;
}
// 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("Beans.xml");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressList();
jc.getAddressSet();
jc.getAddressMap();
jc.getAddressProp();
}
}
以下是包含所有型別集合配置的配置檔案Beans.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>Pakistan</value>
<value>USA</value>
<value>USA</value>
</list>
</property>
<!-- results in a setAddressSet(java.util.Set) call -->
<property name = "addressSet">
<set>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</set>
</property>
<!-- results in a setAddressMap(java.util.Map) call -->
<property name = "addressMap">
<map>
<entry key = "1" value = "INDIA"/>
<entry key = "2" value = "Pakistan"/>
<entry key = "3" value = "USA"/>
<entry key = "4" value = "USA"/>
</map>
</property>
<!-- results in a setAddressProp(java.util.Properties) call -->
<property name = "addressProp">
<props>
<prop key = "one">INDIA</prop>
<prop key = "one">INDIA</prop>
<prop key = "two">Pakistan</prop>
<prop key = "three">USA</prop>
<prop key = "four">USA</prop>
</props>
</property>
</bean>
</beans>
建立原始檔和bean配置檔案後,讓我們執行應用程式。如果您的應用程式一切正常,它將列印以下訊息:
List Elements :[INDIA, Pakistan, USA, USA]
Set Elements :[INDIA, Pakistan, USA]
ap Elements :{1 = INDIA, 2 = Pakistan, 3 = USA, 4 = USA}
Property Elements :{two = Pakistan, one = INDIA, three = USA, four = USA}
注入Bean引用
以下Bean定義將幫助您瞭解如何將bean引用作為集合元素之一進行注入。您甚至可以像以下程式碼片段中所示的那樣混合引用和值:
<?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 Definition to handle references and values -->
<bean id = "..." class = "...">
<!-- Passing bean reference for java.util.List -->
<property name = "addressList">
<list>
<ref bean = "address1"/>
<ref bean = "address2"/>
<value>Pakistan</value>
</list>
</property>
<!-- Passing bean reference for java.util.Set -->
<property name = "addressSet">
<set>
<ref bean = "address1"/>
<ref bean = "address2"/>
<value>Pakistan</value>
</set>
</property>
<!-- Passing bean reference for java.util.Map -->
<property name = "addressMap">
<map>
<entry key = "one" value = "INDIA"/>
<entry key = "two" value-ref = "address1"/>
<entry key = "three" value-ref = "address2"/>
</map>
</property>
</bean>
</beans>
要使用上述bean定義,您需要以能夠處理引用的方式定義setter方法。
注入空值和空字串
如果需要傳遞空字串作為值,則可以按如下方式傳遞:
<bean id = "..." class = "exampleBean"> <property name = "email" value = ""/> </bean>
上面的示例等效於Java程式碼:exampleBean.setEmail("")
如果需要傳遞NULL值,則可以按如下方式傳遞:
<bean id = "..." class = "exampleBean"> <property name = "email"><null/></property> </bean>
上面的示例等效於Java程式碼:exampleBean.setEmail(null)