Spring装配集合的四个方法及实现

四种装配集合

在这我我直接用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();

//得到这个结合键值的keyset集合

Set<String>setProp=properties.stringPropertyNames();

Iterator<String>iterator=setProp.iterator();

while(iterator.hasNext()){

Stringkey=iterator.next();

System.out.println(key+"-----"+properties.getProperty(key));

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值