这里说说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. C++中巧妙的位运算

    位运算要多想到与预算和异或运算,并常常将两个数对应位上相同和不同分开处理 一.x&(x-1)消除x二进制中最右边的一个1. 这个比较厉害,比如统计某个 二.与和异或的巧妙结合的思想 与运算可以 ...

  2. apache开源项目--HIVE

    Hive是一个基于Hadoop的数据仓库平台.通过hive,我们可以方便地进行ETL的工作.hive定义了一个类似于SQL的查询语言:HQL,能 够将用户编写的QL转化为相应的Mapreduce程序基 ...

  3. shell编程——if语句 if -z -n -f -eq -ne -lt

    if  条件then Commandelse Commandfi                              别忘了这个结尾 If语句忘了结尾fitest.sh: line 14: sy ...

  4. 【转】编译Android系统源码和内核源码

    原文网址:http://blog.csdn.net/jiangwei0910410003/article/details/37988637 好长时间没有写blog了,之所以没有写,主要还是工作上的事, ...

  5. PHP错误:Forbidden You don't have permission to access / on this server.

    今天在测试一个php程序的时候,发现这个问题: Forbidden You don't have permission to access / on this server. 开始的时候我是用http ...

  6. Java多线程同步——生产者消费者问题

    这是马士兵老师的Java视频教程里的一个生产者消费者问题的模型 public class ProduceConsumer{ public static void main(String[] args) ...

  7. ARM Linux系统的时钟机制

    1. Linux下有两类时钟: 1.1 实时钟RTC 它由板上电池驱动的“Real Time Clock”也叫做RTC或者叫CMOS时钟,硬件时钟.当操作系统关机的时候,用这个来记录时间,但是对于运行 ...

  8. Swift数组的加法运算符用法:array1 += array2

    var stringList1 = [String]() //创建String类型空数组 var stringList2 = ["1", "3", " ...

  9. BlockingQueue汇总

    前言: 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便 ...

  10. POJ3087 Shuffle'm Up 简单模拟

    题意:就是给你两副扑克,然后一张盖一张洗牌,不断重复这个过程,看能不能达到目标的扑克顺序 分析:然后就模拟下,-1的情况就是有循环节 #include<cstdio> #include&l ...