作用:主要用于框架整合配置。
Java.utils包中常用集合
(1)List
(2)Set
(3)Map
(4)Properties
Spring为集合提供了对应的标签:
注入 list元素
第一步:创建类CollectionBean.java,并提供set方法用作集合的注入
public class CollectionBean {
private List
private Set
private Map
private Properties properties;//特殊类型的map,key和value都是String
public void setList(List
this.list = list;
}
public void setSet(Set
this.set = set;
}
public void setMap(Map
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public String toString() {
return "CollectionBean [list=" + list + ", set=" + set + ", map=" + map
+ ", properties=" + properties + "]";
}
}
第二步:配置spring的核心容器applicationContext.xml
<bean id="collectionBean" class="com.igeek.xmlpropertydi.CollectionBean">
<property name="list">
<list>
<value>Tomvalue>
<value>Jackvalue>
list>
property>
<property name="set">
<set>
<value>12value>
<value>15value>
set>
property>
<property name="map">
<map>
<entry key="name" value="张三"/>
<entry key="age" value="22"/>
<entry key="car" value-ref="car">entry>
map>
property>
<property name="properties">
<props>
<prop key="name">李四prop>
<prop key="age">33prop>
props>
property>
bean>
第三步:使用SpringTest类进行测试
@Test
public void test5(){
//spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean collectionBean=(CollectionBean)applicationContext.getBean("collectionBean");
System.out.println(collectionBean);
}
第四步:输出结果: