Spring笔记之二(Collection Injection)

转贴处 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 /> --> 1 import java.util.Iterator;
2 import java.util.List;
3 import java.util.Map;
4 import java.util.Properties;
5 import java.util.Set;
6
7 import org.springframework.beans.factory.BeanFactory;
8 import org.springframework.beans.factory.xml.XmlBeanFactory;
9 import org.springframework.core.io.FileSystemResource;
10
11 public class CollectionInjection{
12
13 private Mapmap;
14
15 private Propertiesprops;
16
17 private Setset;
18
19 private Listlist;
20
21 public static void main(String[]args){
22 BeanFactoryfactory = new XmlBeanFactory( new FileSystemResource(
23 " src/applicationContext.xml " ));
24
25 CollectionInjectioninstance = (CollectionInjection)
factory.getBean(
" injectCollection " );
26 instance.displayInfo();
27 }
28
29 public void setList(Listlist){
30 this .list = list;
31 }
32
33 public void setSet(Setset){
34 this .set = set;
35 }
36
37 public void setMap(Mapmap){
38 this .map = map;
39 }
40
41 public void setProps(Propertiesprops){
42 this .props = props;
43 }
44
45 public void displayInfo(){
46
47 // displaytheMap
48 Iteratori = map.keySet().iterator();
49
50 System.out.println( " Mapcontents:/n " );
51 while (i.hasNext()){
52 Objectkey = i.next();
53 System.out.println( " Key: " + key + " -Value: " + map.get(key));
54 }
55
56 // displaytheproperties
57 i = props.keySet().iterator();
58 System.out.println( " /nPropertiescontents:/n " );
59 while (i.hasNext()){
60 Stringkey = i.next().toString();
61 System.out.println( " Key: " + key + " -Value: "
62 + props.getProperty(key));
63 }
64
65 // displaytheset
66 i = set.iterator();
67 System.out.println( " /nSetcontents:/n " );
68 while (i.hasNext()){
69 System.out.println( " Value: " + i.next());
70 }
71
72 // displaythelist
73 i = list.iterator();
74 System.out.println( " /nListcontents:/n " );
75 while (i.hasNext()){
76 System.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 /> --> 1 public class RefBean{
2 public StringtoString()
3 {
4 return " 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 < bean id ="refBean" name ="refB" class ="RefBean" />
7
8 <!-- collectioninjectionsamples -->
9 < bean id ="injectCollection" class ="CollectionInjection" >
10 < property name ="map" >
11 < map >
12 < entry key ="nameValue" >
13 < value > gzhb_improviser </ value >
14 </ entry >
15 < entry key ="refBeanValue" >
16 < ref local ="refBean" />
17 </ entry >
18 </ map >
19 </ property >
20 < property name ="props" >
21 < props >
22 < prop key ="firstName" > gzhb </ prop >
23 < prop key ="secondName" > improviser </ prop >
24 </ props >
25 </ property >
26 < property name ="set" >
27 < set >
28 < value > gzhb </ value >
29 < ref local ="refBean" />
30 </ set >
31 </ property >
32 < property name ="list" >
33 < list >
34 < value > improviser </ value >
35 < ref local ="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.
< bean id ="parent" abstract ="true" class ="example.ComplexObject" >
3.
< property name ="pro" >
4.
< props >
5.
< prop key ="pro1" > pro1 </ prop >
6.
< prop key ="pro3" >pro3 </ prop >
7.
</ props >
8.
</ property >
9.
</ bean >
10.
< bean id ="child" parent ="parent" >
11.
< property name ="pro" >
12.
<!-- themergeisspecifiedonthe*child*collectiondefinition -->
13.
< props merge ="true" >
14.
< prop key ="pro2" > pro2 </ prop >
15.
< prop key ="pro3" > pro3 </ prop >
16.
</ props >
17.
</ property >
18.
</ bean >
19.
< beans >

通过合并,子bean的pro3值覆盖父bean的值,不同集合类型不能合并,子bean中的merge必须定义,否则出现异常,

你可能感兴趣的:(Collection)