注入集合
?你已經(jīng)看到了如何使用value屬性來配置基本數(shù)據(jù)類型和在你的bean配置文件中使用標(biāo)簽ref屬性來配置對象引用.
如果你項傳遞多個值,如Java Collection 類型 List , Set , Map , Properties 應(yīng)該怎么做呢. 為了處理這種情況 , Spring 提供了四種類型的集合的配置元素,如下所示:
| 元素 | 描述 |
|---|---|
| <list> | 它有助于練習(xí),如注入一系列值,允許重復(fù). |
| <set> | 它有助于連線一組值,但不能重復(fù). |
| <map> | 它可以用來注入名稱 - 鍵值對的集合,其中名稱和值可以是任何類型. |
| <props> | 它可以用來注入名稱 - 鍵值對結(jié)合,其中名稱和值都是字符串類型 |
你可以使用或來連接任何java.util.Collection的實現(xiàn)或數(shù)組
你會遇到兩種情況(a)傳遞集合中直接(b)傳遞一個bean的引用作為集合的元素.
例子
| 步驟 | 描述 |
|---|---|
| 1 | 創(chuàng)建一個名稱為 SpringExample 的項目,并且在創(chuàng)建項目的 c src 文件夾中創(chuàng)建一個包 com.tutorialspoint |
| 2 | 使用 Add External JARs 選項,添加所需的 Spring 庫,解釋見 Spring Hello World Example 章節(jié)。 option as explained in the chapter. |
| 3 | 在 com.tutorialspoint 包中創(chuàng)建Java類 TextEditor 、 SpellChecker 和 MainApp. |
| 4 | 在src 文件夾中創(chuàng)建 Beans 配置文件 Beans.xml 。 |
| 5 | 最后一步是創(chuàng)建的所有Java文件和Bean配置文件的內(nèi)容,并運行應(yīng)用程序,解釋如下所示。 |
JavaCollection.java
package com.tutorialspoint;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class JavaCollection {
List addressList;
Set addressSet;
Map addressMap;
Properties addressProp;
public List getAddressList() {
System.out.println("List Elements :" + addressList);
return addressList;
}
public void setAddressList(List addressList) {
this.addressList = addressList;
}
public Set getAddressSet() {
System.out.println("Set Elements :" + addressSet);
return addressSet;
}
public void setAddressSet(Set addressSet) {
this.addressSet = addressSet;
}
public Map getAddressMap() {
System.out.println("Map Elements :" + addressMap);
return addressMap;
}
public void setAddressMap(Map addressMap) {
this.addressMap = addressMap;
}
public Properties getAddressProp() {
System.out.println("Property Elements :" + addressProp);
return addressProp;
}
public void setAddressProp(Properties addressProp) {
this.addressProp = 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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<bean id="javaCollection" class="com.tutorialspoint.JavaCollection">
<property name="addressList">
<list>
<value>india</value>
<value>pakistan</value>
<value>usa</value>
<value>usa</value>
</list>
</property>
<property name="addressSet">
<set>
<value>india</value>
<value>pakistan</value>
<value>usa</value>
<value>usa</value>
</set>
</property>
<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>
<property name="addressProp">
<props>
<prop key="one">INDIA</prop>
<prop key="two">Pakistan</prop>
<prop key="three">USA</prop>
<prop key="four">USA</prop>
</props>
</property>
</bean>
</beans>
測試跑通之后的結(jié)果:
List Elements :[india, pakistan, usa, usa]
Set Elements :[india, pakistan, usa]
Map Elements :{1=INDIA, 2=Pakistan, 3=USA, 4=USA}
Property Elements :{two=Pakistan, one=INDIA, three=USA, four=USA}
個人小結(jié):其實和上面的差不多,都是創(chuàng)建對象,然后注入,再取出的操作.