四种装配集合
在这我我直接用utilschema集合,用util标签
只需要把util的约束加进去就行,引进其他的也是这样直接加就可以了
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
第一种Set集合
<bean
id="collectionBean"class="www.csdn.spring01.collection.CollectionBean">
<propertyname="sets">
<!--set集合如果不引用utilschema集合直接写<set>集合就行-->
<util:set>
<value>洋1</value>
<value>胖2</value>
<value>小3</value>
<value>杨4</value>
<value>小5</value>
<value>恢6</value>
</util:set>
</property>
第二种list集合
<propertyname="lists">
<util:list>
<refbean="u1"/>
<refbean="u2"/>
<refbean="u3"/>
</util:list>
</property>
第三种map集合
<propertyname="maps">
<util:map>
<entrykey="1"value-ref="u1"/>
<entrykey="2"value-ref="u2"/>
<entrykey="3">
<refbean="u3"/>
</entry>
</util:map>
</property>
第四种properties集合
<propertyname="properties">
<util:properties>
<propkey="1">JDBC:ORACLE</prop>
<propkey="2">JDBC:MYSQL</prop>
<propkey="3">JDBC:SQL</prop>
</util:properties>
</property>
</bean>
引用的其他的类
<beanid="u1"class="www.csdn.spring01.collection.User">
<propertyname="name"value="刘--"/>
<propertyname="age"value="11"/>
</bean>
<beanid="u2"class="www.csdn.spring01.collection.User">
<propertyname="name"value="2--"/>
<propertyname="age"value="22"/>
</bean>
<beanid="u3"class="www.csdn.spring01.collection.User">
<propertyname="name"value="2--"/>
<propertyname="age"value="33"/>
</bean>
Bean类
publicclassCollectionBean{
//--------set----------
privateSet<String>sets;;
publicSet<String>getSets(){
returnsets;
}
publicvoidsetSets(Set<String>sets){
this.sets=sets;
}
//--------list----------
privateList<User>lists;
publicList<User>getLists(){
returnlists;
}
publicvoidsetLists(List<User>lists){
this.lists=lists;
}
//--------map-----------
privateMap<Integer,User>maps;
publicMap<Integer,User>getMaps(){
returnmaps;
}
publicvoidsetMaps(Map<Integer,User>maps){
this.maps=maps;
}
//------properties-------
privatePropertiesproperties;
publicPropertiesgetProperties(){
returnproperties;
}
publicvoidsetProperties(Propertiesproperties){
this.properties=properties;
}
packagewww.csdn.spring01.collection;
publicclassUser{
privateStringname;
privateIntegerage;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicIntegergetAge(){
returnage;
}
publicvoidsetAge(Integerage){
this.age=age;
}
}
测试方法:
packagewww.csdn.spring01.collection;
importstaticorg.junit.Assert.*;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importjava.util.Map.Entry;
importjava.util.Properties;
importjava.util.Set;
importorg.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassCollectionTest{
@Test
publicvoidtestSet(){
ApplicationContextcontext=newClassPathXmlApplicationContext(
"classpath:spring-collection.xml");
CollectionBeancollectionBean=context.getBean("collectionBean",
CollectionBean.class);
Set<String>sets=collectionBean.getSets();
Iteratoriterator=sets.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
publicvoidtestList(){
ApplicationContextcontext=newClassPathXmlApplicationContext(
"classpath:spring-collection.xml");
CollectionBeancollectionBean=context.getBean("collectionBean",
CollectionBean.class);
List<User>lists=collectionBean.getLists();
for(Userlist:lists){
System.out.println(list.getName()+"-------"+list.getAge());
}
}
@Test
publicvoidtestMap(){
ApplicationContextcontext=newClassPathXmlApplicationContext(
"classpath:spring-collection.xml");
CollectionBeancollectionBean=context.getBean("collectionBean",
CollectionBean.class);
Map<Integer,User>maps=collectionBean.getMaps();
//--通过set方法获取到所有的key
Set<Integer>setKeys=maps.keySet();
//--遍历key
Iterator<Integer>iterator=setKeys.iterator();
while(iterator.hasNext()){
//得到一个具体的键值
Integerkey=iterator.next();
//通过map集合的get(key)方法获取key的值
Useruser=maps.get(key);
System.out.println(key+"------"+user.getName()+"-----"
+user.getAge());
}
}
@Test
publicvoidtestMap2(){
ApplicationContextcontext=newClassPathXmlApplicationContext(
"classpath:spring-collection.xml");
CollectionBeancollectionBean=context.getBean("collectionBean",
CollectionBean.class);
Map<Integer,User>maps=collectionBean.getMaps();
//--通过set方法获取到所有的key
Set<Entry<Integer,User>>setKeys=maps.entrySet();
//--遍历key
Iterator<Entry<Integer,User>>iterator=setKeys.iterator();
while(iterator.hasNext()){
//得到一个具体的键值
Entry<Integer,User>entry=iterator.next();
//通过map集合的get(key)方法获取key的值
System.out.println(entry.getKey()+"------"+entry.getValue().getName()+"-----"
+entry.getValue().getAge());
}
}
@Test
publicvoidtestProperties(){
ApplicationContextcontext=newClassPathXmlApplicationContext(
"classpath:spring-collection.xml");
CollectionBeancollectionBean=context.getBean("collectionBean",
CollectionBean.class);
Propertiesproperties=collectionBean.getProperties();
//得到这个结合键值的key的set集合
Set<String>setProp=properties.stringPropertyNames();
Iterator<String>iterator=setProp.iterator();
while(iterator.hasNext()){
Stringkey=iterator.next();
System.out.println(key+"-----"+properties.getProperty(key));
}
}
}