转贴处 http://www.blogjava.net/improviser/archive/2007/09/24/147710.html
通过<list/>,<set/>,<map/>以及<props/>元素定义和设置与java collection类型对应的List,Set,Map,Rproperties的值。
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->
1importjava.util.Iterator;
2importjava.util.List;
3importjava.util.Map;
4importjava.util.Properties;
5importjava.util.Set;
6
7importorg.springframework.beans.factory.BeanFactory;
8importorg.springframework.beans.factory.xml.XmlBeanFactory;
9importorg.springframework.core.io.FileSystemResource;
10
11publicclassCollectionInjection{
12
13privateMapmap;
14
15privatePropertiesprops;
16
17privateSetset;
18
19privateListlist;
20
21publicstaticvoidmain(String[]args){
22BeanFactoryfactory=newXmlBeanFactory(newFileSystemResource(
23"src/applicationContext.xml"));
24
25CollectionInjectioninstance=(CollectionInjection)
factory.getBean("injectCollection");
26instance.displayInfo();
27}
28
29publicvoidsetList(Listlist){
30this.list=list;
31}
32
33publicvoidsetSet(Setset){
34this.set=set;
35}
36
37publicvoidsetMap(Mapmap){
38this.map=map;
39}
40
41publicvoidsetProps(Propertiesprops){
42this.props=props;
43}
44
45publicvoiddisplayInfo(){
46
47//displaytheMap
48Iteratori=map.keySet().iterator();
49
50System.out.println("Mapcontents:/n");
51while(i.hasNext()){
52Objectkey=i.next();
53System.out.println("Key:"+key+"-Value:"+map.get(key));
54}
55
56//displaytheproperties
57i=props.keySet().iterator();
58System.out.println("/nPropertiescontents:/n");
59while(i.hasNext()){
60Stringkey=i.next().toString();
61System.out.println("Key:"+key+"-Value:"
62+props.getProperty(key));
63}
64
65//displaytheset
66i=set.iterator();
67System.out.println("/nSetcontents:/n");
68while(i.hasNext()){
69System.out.println("Value:"+i.next());
70}
71
72//displaythelist
73i=list.iterator();
74System.out.println("/nListcontents:/n");
75while(i.hasNext()){
76System.out.println("Value:"+i.next());
77}
78}
79}
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->1publicclassRefBean{
2publicStringtoString()
3{
4return"refBean";
5}
6}
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->1<?xmlversion="1.0"encoding="UTF-8"?>
2<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
3<beans>
4
5<!--oraclebeanusedforafewexamples-->
6<beanid="refBean"name="refB"class="RefBean"/>
7
8<!--collectioninjectionsamples-->
9<beanid="injectCollection"class="CollectionInjection">
10<propertyname="map">
11<map>
12<entrykey="nameValue">
13<value>gzhb_improviser</value>
14</entry>
15<entrykey="refBeanValue">
16<reflocal="refBean"/>
17</entry>
18</map>
19</property>
20<propertyname="props">
21<props>
22<propkey="firstName">gzhb</prop>
23<propkey="secondName">improviser</prop>
24</props>
25</property>
26<propertyname="set">
27<set>
28<value>gzhb</value>
29<reflocal="refBean"/>
30</set>
31</property>
32<propertyname="list">
33<list>
34<value>improviser</value>
35<reflocal="refBean"/>
36</list>
37</property>
38</bean>
39</beans
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importjava.util.Properties;
importjava.util.Set;
importorg.springframework.beans.factory.BeanFactory;
importorg.springframework.beans.factory.xml.XmlBeanFactory;
importorg.springframework.core.io.FileSystemResource;
publicclassCollectionInjection{
privateMapmap;
privatePropertiesprops;
privateSetset;
privateListlist;
publicstaticvoidmain(String[]args){
BeanFactoryfactory=newXmlBeanFactory(newFileSystemResource(
"src/applicationContext.xml"));
CollectionInjectioninstance=(CollectionInjection)factory.getBean("injectCollection");
instance.displayInfo();
}
publicvoidsetList(Listlist){
this.list=list;
}
publicvoidsetSet(Setset){
this.set=set;
}
publicvoidsetMap(Mapmap){
this.map=map;
}
publicvoidsetProps(Propertiesprops){
this.props=props;
}
publicvoiddisplayInfo(){
//displaytheMap
Iteratori=map.keySet().iterator();
System.out.println("Mapcontents:/n");
while(i.hasNext()){
Objectkey=i.next();
System.out.println("Key:"+key+"-Value:"+map.get(key));
}
//displaytheproperties
i=props.keySet().iterator();
System.out.println("/nPropertiescontents:/n");
while(i.hasNext()){
Stringkey=i.next().toString();
System.out.println("Key:"+key+"-Value:"
+props.getProperty(key));
}
//displaytheset
i=set.iterator();
System.out.println("/nSetcontents:/n");
while(i.hasNext()){
System.out.println("Value:"+i.next());
}
//displaythelist
i=list.iterator();
System.out.println("/nListcontents:/n");
while(i.hasNext()){
System.out.println("Value:"+i.next());
}
}
}
运行结果:Map contents:
Key: nameValue - Value: gzhb_improviser
Key: refBeanValue - Value: refBean
Properties contents:
Key: secondName - Value: improviser
Key: firstName - Value: gzhb
Set contents:
Value: gzhb
Value: refBean
List contents:
Value: improviser
Value: refBean
注意:
map的key或value值,或set的value值不能一下元素:bean,ref,idref,list,set,map,props,vaues,null
集合合并
Spring2.0支持集合合并,子类Bean实例可以合并和重写抽象父类Bean实例的集合属性。
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->1.<beans>
2.<beanid="parent"abstract="true"class="example.ComplexObject">
3.<propertyname="pro">
4.<props>
5.<propkey="pro1">pro1</prop>
6.<propkey="pro3">pro3</prop>
7.</props>
8.</property>
9.</bean>
10.<beanid="child"parent="parent">
11.<propertyname="pro">
12.<!--themergeisspecifiedonthe*child*collectiondefinition-->
13.<propsmerge="true">
14.<propkey="pro2">pro2</prop>
15.<propkey="pro3">pro3</prop>
16.</props>
17.</property>
18.</bean>
19.<beans>
通过合并,子bean的pro3值覆盖父bean的值,不同集合类型不能合并,子bean中的merge必须定义,否则出现异常,
分享到:
相关推荐
SPRING 笔记SPRING 笔记SPRING 笔记
spring笔记spring笔记spring笔记spring笔记spring笔记spring笔记
达内,tarena,spring笔记,springPPT课件,达内spring笔记 spring的全套资料
spring入门笔记
Spring笔记 Spring笔记 Spring笔记 Spring笔记 Spring笔记
Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Spring...
spring笔记 狂神说
spring笔记.pdf
Spring笔记示例源代码
尚硅谷Spring笔记
尚硅谷Spring6的笔记
介绍了SpringCloud组件的使用
Spring学习笔记,Spring就是一个工厂,可以将所有对象创建和依赖关系维护,交给spring管理
Spring学习笔记( spring视频笔记)
spring 初学 笔记 入门提示
二、 第二步: 首先整合Spring + Hibernate 41 三、 第三步:再来整合Struts2 41 四、 struts的读常量: 43 第十二课:DTO、VO 43 一、 DTO 43 二、 VO 43 第十二课:SSH整合存在的问题 43 一、 Jsp中访问Session时...
spring学习笔记spring学习笔记spring学习笔记spring学习笔记spring学习笔记spring学习笔记spring学习笔记spring学习笔记
韩顺平Spring笔记之ssh整合.doc
网上获取的简单关于spring的一些学习笔记, 包括 spring, springMVC, spring cloud, springBoot 和一些其他相关的笔记, 有需要的可以看看