`
jiqimiao
  • 浏览: 63182 次
  • 性别: Icon_minigender_1
  • 来自: 常州
社区版块
存档分类
最新评论

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 /> -->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 笔记

    尚硅谷SpringCloud第二季笔记

    【尚硅谷SpringCloud第二季笔记】是一份详细记录了SpringCloud组件使用的学习笔记,涵盖了SpringCloud在云原生环境下的核心概念和实践应用。SpringCloud是基于Spring Boot实现的微服务框架,它提供了构建分布式系统...

    spring笔记.pdf

    spring笔记spring基础笔记

    spring笔记.md

    spring入门笔记

    达内,tarena,spring笔记,springPPT课件,达内spring笔记

    1. **IoC(Inversion of Control)容器**:Spring的核心特性之一,通过反转控制权,让框架负责管理对象的生命周期和装配,开发者只需定义配置,无需手动创建对象。 2. **依赖注入(Dependency Injection, DI)**:...

    Spring笔记.doc

    它的核心特性是依赖注入(Dependency Injection,简称 DI)和面向切面编程(Aspect-Oriented Programming,简称 AOP)。Spring 的轻量级特性意味着它不强求使用特定的数据库或技术,因此它具有很好的可扩展性。 1. ...

    Spring笔记示例源代码

    Spring框架是Java开发中最常用的轻量级开源框架之一,它以IoC(Inversion of Control,控制反转)和AOP(Aspect Oriented Programming,面向切面编程)为核心,极大地简化了企业级应用的开发工作。"Spring笔记示例源...

    Springcloud学习笔记.md

    Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Spring...

    Spring笔记.docx

    1. **控制反转(Inversion of Control, IoC)**:IoC是Spring的核心特性之一,通过这种方式,对象的创建和管理交由Spring容器负责,从而降低了组件间的耦合度。 2. **依赖注入(Dependency Injection, DI)**:DI是...

    尚硅谷Spring笔记

    尚硅谷Spring笔记

    spring笔记.zip

    - **依赖注入(Dependency Injection, DI)**:Spring的核心特性,允许对象之间的关系在运行时被定义和管理,减少了代码间的耦合。 - **容器**:Spring容器负责创建对象,管理它们的生命周期,以及装配这些对象。...

    spring笔记.rar

    在Java开发领域,Spring框架无疑是最具影响力和广泛使用的轻量级框架之一。它以其强大的功能、灵活的设计和丰富的生态系统,成为了企业级应用开发的首选。本文将根据传智播客左慈老师的培训笔记,对Spring框架的核心...

    Spring笔记 狂神说

    spring笔记 狂神说

    Spring学习笔记+学习源码.zip

    1. **依赖注入(Dependency Injection,DI)**:Spring通过DI帮助开发者管理对象之间的依赖关系,降低了组件间的耦合度。你可以通过构造器注入、设值注入或接口注入三种方式实现。 2. **容器(IoC Container)**:...

    Spring学习笔记(精华全记录)

    ### Spring学习笔记(精华全记录) #### Spring框架概述 Spring框架源自Rod Johnson的个人项目,最初于2002年末发布。Spring并非一开始就作为一个完整的框架出现,而是从一个项目逐步发展而来。随着项目的成熟,...

    尚硅谷Spring6的笔记

    尚硅谷Spring6的笔记

    Spring学习笔记( spring视频笔记)

    Spring学习笔记( spring视频笔记)

    【狂神说】Spring全面详细笔记.md

    2. **Spring Context**:建立在Core之上,提供了一个配置文件的加载机制及事件传播机制。 3. **Spring AOP**:提供了面向切面编程的实现,让开发者能够更加方便地实现诸如事务管理等功能。 4. **Spring DAO**:提供...

    spring 笔记

    spring 初学 笔记 入门提示

Global site tag (gtag.js) - Google Analytics