这里说说bean装配集合。spring的支持的集合元素,其基本使用方式如同与Java的集合,所以假设对Java的集合不太了解的能够先找个帖子好好学习一下, 时间关系这里就不说了。

~~

list的样例

public interface Instrument {
void play();
}
public class Guitar implements Instrument {

    @Override
public void play() {
// TODO Auto-generated method stub
System.out.println("dang dang dang");
} }
public class Piano implements Instrument{

    @Override
public void play() {
// TODO Auto-generated method stub
System.out.println("ding ding ding");
} }
public class Saxophone implements Instrument{

    @Override
public void play() {
// TODO Auto-generated method stub
System.out.println("wu wu wu ");
} }
public class Band {
private Collection<Instrument> instrument; public Collection<Instrument> getInstrument() {
return instrument;
} public void setInstrument(Collection<Instrument> instrument) {
this.instrument = instrument;
} public void play(){
for(Instrument ins:instrument)
ins.play();
}
}
public class test {

    public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("blog3/bean.xml");
Band band = (Band) ac.getBean("band");
band.play();
} }
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="band" class="blog3.Band">
<property name="instrument">
<list>
<ref bean="guitar"/>
<ref bean="piano"/>
<ref bean="saxophone"/>
</list>
</property>
</bean> <bean id="guitar" class="blog3.Guitar"/>
<bean id="piano" class="blog3.Piano"/>
<bean id="saxophone" class="blog3.Saxophone"/>

set和list使用方式一致。把list换成set就好了,主要是set中元素不能反复。另外多说一点,为了解耦Spring推荐使用基于接口的编程方式。

另外数组也是使用这样的装配方式。

map

<bean id="bandmap" class="blog3.Band">
<property name="mapIns">
<map>
<entry key="GUITAR" value-ref="guitar"/>
<entry key="PINAO" value-ref="piano"/>
<entry key="SAXOPHONE" value-ref="saxophone"/>
</map>
</property>
</bean>
public class Band {
private Collection<Instrument> instrument;
private Map<String, Instrument> mapIns; public Map<String, Instrument> getMapIns() {
return mapIns;
} public void setMapIns(Map<String, Instrument> mapIns) {
this.mapIns = mapIns;
} public Collection<Instrument> getInstrument() {
return instrument;
} public void setInstrument(Collection<Instrument> instrument) {
this.instrument = instrument;
} public void play(){
for(Instrument ins:instrument)
ins.play();
} public void mapPlay(){
for(String key:mapIns.keySet()){
System.out.println(key);
Instrument tmp = mapIns.get(key);
tmp.play();
} }
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("blog3/bean.xml");
// Band band = (Band) ac.getBean("band");
// band.play(); Band band2 = (Band) ac.getBean("bandmap");
band2.mapPlay();
}

properties装配,对照与map,properties的key和value仅仅能是string类型。其余都差点儿相同

好了,差点儿相同了。上代码吧。

http://download.csdn.net/detail/wsrspirit/8868347

Spring 从零開始-03的更多相关文章

  1. Spring 从零開始-05

    最终能到Spring的AOP编程了,AOP的概念特别的多.所以须要你在開始之前有点了解,然后通过代码慢慢学习! - 切面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象.事务管 ...

  2. 从零開始学习OpenCL开发(一)架构

    多谢大家关注 转载本文请注明:http://blog.csdn.net/leonwei/article/details/8880012 本文将作为我<从零開始做OpenCL开发>系列文章的 ...

  3. 从零開始学Swift之Hello World进化版

    上节课,也就是昨晚啦,我们学习到从零開始学Swift之Hello World.那一节仅仅有一句代码,大家会认为不够过瘾. 那么这节课,就给大家来多点瘾货吧! 先上图! //var 代表变量的类型, s ...

  4. 关东升的《从零開始学Swift》即将出版

    大家好: 苹果2015WWDC大会公布了Swift2.0,它较之前的版本号Swift1.x有非常大的变化.所以我即将出版<从零開始学Swift><从零開始学Swift>将在&l ...

  5. # 从零開始搭建Hadoop2.7.1的分布式集群

    Hadoop 2.7.1 (2015-7-6更新),Hadoop的环境配置不是特别的复杂,可是确实有非常多细节须要注意.不然会造成很多配置错误的情况.尽量保证一次配置正确防止重复改动. 网上教程有非常 ...

  6. Bmob移动后端云服务平台--Android从零開始--(二)android高速入门

    Bmob移动后端云服务平台--Android从零開始--(二)android高速入门 上一篇博文我们简介何为Bmob移动后端服务平台,以及其相关功能和优势. 本文将利用Bmob高速实现简单样例,进一步 ...

  7. 从零開始怎么写android native service?

    从零開始怎么写android native service Android service对于从事android开发的人都不是一个陌生的东西,非常多人可能会认为服务非常easy. 服务是简单,由于复杂 ...

  8. Bmob移动后端云服务平台--Android从零開始--(一)何为Bmob

    Bmob移动后端云服务平台--Android从零開始--(一)何为Bmob 在正式的项目开发中,单client不能满足我们的需求,须要实现client与服务端的连接. 而在编写Android服务端代码 ...

  9. 《PHP 5.5从零開始学(视频教学版)》内容简单介绍、文件夹

    <PHP 5.5从零開始学(视频教学版)>当当网购买地址: http://product.dangdang.com/23586810.html <PHP 5.5从零開始学(视频教学版 ...

随机推荐

  1. Servlet个人总结

    netstat -an ——查看端口占用情况 netstat -an ——查看是谁占用了哪个端口 端口被占用之后可以关闭端口占用程序或者在conf/server.xml修改本身使用端口 javac - ...

  2. mysql 幻读

    幻读(Phantom Read) 是指当用户读取某一范围的数据行时,B事务在该范围内插入了新行,当用户再读取该范围的数据行时,会发现有新的“幻影”行.InnoDB和Falcon存储引擎通 过多版本并发 ...

  3. BZOJ_1027_[JSOI2007]_合金_(计算几何+Floyd求最小环)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1027 共三种金属,\(m\)种材料,给出每种材料中三种金属的占比. 给出\(n\)种合金的三种 ...

  4. HNOI2004打鼹鼠(LIS)

    大水题…… 不过通过这题我们应该养成一个好习惯:好好看清题…… 竟然没有看到时限 10sec…… var i,j,n,m,ans:longint; f,time,x,y:..] of longint; ...

  5. [POJ 3788] Interior Points of Lattice Polygons

    同swustoj 169 Interior Points of Lattice Polygons Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  6. 移植mysql到嵌入式ARM平台

    移植MySQL到嵌入式ARM平台  MySQL没有专门针对ARM的版本,移植到ARM没有官方文档可参考,因此,暂时参考这样一篇文档: http://blog.chinaunix.net/space.p ...

  7. CSS和SVG中的剪切——clip-path属性和<clipPath>元素

    剪切是什么 剪切是一个图形化操作,你可以部分或者完全隐藏一个元素.被剪切的元素可以是一个容器也可以是一个图像元素.元素的哪些部分显示或隐藏是由剪切的路径来决定的. 剪切路径定义了一个区域,在这个区域内 ...

  8. 黑盒测试用例设计方法&理论结合实际 -> 正交试验法

    一. 概念 依据Galois理论,从大量的(实验)数据(测试例)中挑选适量的,有代表性的点(例),从而合理地安排实验(测试)的一种科学实验设计方法.类似的方法有:聚类分析方法,因子方法方法等. 二. ...

  9. 【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate

    UVAlive 4794 Sharing Chocolate 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055 ...

  10. BZOJ 1079 [SCOI2008]着色方案

    http://www.lydsy.com/JudgeOnline/problem.php?id=1079 思路:如果把每种油漆看成一种状态,O(5^15)不行 DP[a][b][c][d][e][f] ...