在Spring中可以裝配4種集合類型屬性:List、set、Map和Properties。與這四種集合對應的標籤是、、
、。CollectionBean是一個包含上述4種集合類型的JavaBean,代碼如下:
package chapter22;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class CollectionBean { private List myList; private String myArray[]; private Set mySet; private Map myMap; private Properties myProperties; public List getMyList() { return myList; } public void setMyList(List myList) { System.out.println("set List類型:"+myList.getClass().getName()); this.myList = myList; } public String[] getMyArray() { return myArray; } public void setMyArray(String[] myArray) { this.myArray = myArray; } public Set getMySet() { return mySet; } public void setMySet(Set mySet) { System.out.println("set Set類型:"+mySet.getClass().getName()); this.mySet = mySet; } public Map getMyMap() { return myMap; } public void setMyMap(Map myMap) { System.out.println("set Map類型:"+myMap.getClass().getName()); this.myMap = myMap; } public Properties getMyProperties() { return myProperties; } public void setMyProperties(Properties myProperties) { System.out.println("set Properties類型:"+myProperties.getClass().getName()); this.myProperties = myProperties; }}
下面是裝配上述4中集合類型屬性的配置代碼
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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="helloservice" class="chapter22.HelloServiceImpl"> <property name="greeting" value="greeting Xu Wei">property>bean> <bean id="myBean" class="chapter22.MyBean"> <property name="hello"> <ref bean="helloservice" />property> <property name="strName" value="Xu Wei">property>bean> <bean id="CollectionBean" class="chapter22.CollectionBean"> <property name="myList"> <list><value>abcdvalue> <idref bean="myBean">idref> list> property><property name="myMap"> <map> <entry> <key> <value>hellovalue>key> <value>1234value>entry> <entry key="abcd"> <ref bean="helloservice" />entry> <entry> <key> <ref bean="helloservice" >ref>key> <ref bean="myBean" >ref>entry>map>property> <property name="mySet"> <set> <value>testvalue> <ref bean="myBean">ref>set>property> <property name="myProperties"> <props><prop key="abcd">value1prop> <prop key="prop">myPropprop>props>property> <property name="myArray"> <list> <value>myArrayvalue> <idref bean="helloservice" >idref>list>property>bean>beans>
下面的TestCollectionContext.java代碼使用ApplicationContext介面的getBean方法建立了CollectionBean對象,並訪問了相關屬性。
package chapter22;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class TestCollectionBean { public static void main(String args[]) { ApplicationContext ctx=new FileSystemXmlApplicationContext("src//applicationContext.xml");/*在建立ctx對象執行個體以後,applicationContext.xml配置中涉及到的Bean的set方法都被調用了。 *單單執行ApplicationContext ctx=new FileSystemXmlApplicationContext("src//applicationContext.xml"); *的輸出結果是: *log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext). *log4j:WARN Please initialize the log4j system properly. *setGreeting()方法被調用 *setHello()方法被調用 *setStrName()方法被調用 *set List類型:java.util.ArrayList *set Map類型:java.util.LinkedHashMap *set Set類型:java.util.LinkedHashSet *set Properties類型:java.util.Properties */ //建立CollectionBean對象cb。 CollectionBean cb=(CollectionBean)ctx.getBean("CollectionBean"); System.out.println("------測試List------"); for(String s:cb.getMyList()) System.out.println(s); System.out.println("------測試Array------"); for(String s:cb.getMyArray()) System.out.println(s); System.out.println("------測試Set------"); for(Object obj:cb.getMySet()) System.out.println(obj.getClass().getName()); System.out.println("------測試Map------"); //Map其實就是key-value的索引值對。cb.getMyMap().get(key)是要找到key所對應的value。 for(Object key:cb.getMyMap().keySet()) System.out.println(key+"="+cb.getMyMap().get(key)); System.out.println("------測試Properties------"); for(Object key:cb.getMyProperties().keySet()) System.out.println(key+"="+cb.getMyProperties().get(key)); }}
輸出結果:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).log4j:WARN Please initialize the log4j system properly.setGreeting()方法被調用setHello()方法被調用setStrName()方法被調用set List類型:java.util.ArrayListset Map類型:java.util.LinkedHashMapset Set類型:java.util.LinkedHashSetset Properties類型:java.util.Properties------測試List------abcdmyBean------測試Array------myArrayhelloservice------測試Set------java.lang.Stringchapter22.MyBean------測試Map------hello=1234abcd=chapter22.HelloServiceImpl@e70e30chapter22.HelloServiceImpl@e70e30=chapter22.MyBean@154864a------測試Properties------abcd=value1prop=myProp