这里说说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. ListView使用CursorAdapter增加和删除item

    @Override protected void onCreate(Bundle savedInstanceState) { // TODO 自动生成的方法存根 super.onCreate(save ...

  2. sublime text 2 配置文件

    安装好 sublime text 2后不进行任何设置,如果允许 sublime text 2 软件的话,会在以下目录自动生成一个 sublime text 2 的文件夹,这个文件夹是用来放配置文件的, ...

  3. Innodb的启动

    Innodb的启动与关闭 1. 启动函数的流程 innobase_start_or_create_for_mysql 启动的主要步骤包括: 1. 初始化innodb的参数 2. 初始化系统结构 主要结 ...

  4. 记录遇到的IE8兼容性问题汇总

    1,伪元素:first-child不起作用,需要单独指定样式名称 2,透明度表示方法,尤其在函数中表示 @mixin opacity($value) { -webkit-opacity: $value ...

  5. vijos1049送给圣诞夜的礼品

    这题犯了两个sb错误,写下来,为以后做个警告 一.mul过程中将k作为了循环变量 二.看错了题…… 题目中说是数到k行,而我却以为数k遍…… 做矩阵乘法,只要记住一句话:置换一定可以写成矩阵的形式! ...

  6. 关于Azure存储账户中存储虚拟机VHD文件的注意事项

     Joy Qiao from MSFT  Thu, Mar 12 2015 3:16 PM 我们在使用Azure时经常都会在Azure存储账户中放一些文件,包括Azure虚机的VHD文件也都是放在存储 ...

  7. c语言宏定义#define的理解与资料整理

    1. 利用define来定义 数值宏常量 #define 宏定义是个演技非常高超的替身演员,但也会经常耍大牌的,所以我们用它要慎之又慎.它可以出现在代码的任何地方,从本行宏定义开始,以后的代码就就都认 ...

  8. U-boot mkimage指定Linux内核地址时的两种方式

    http://blog.csdn.net/embededswordman/article/details/6704197 uImage的制作是使用的u-boot工具mkimage,build完u-bo ...

  9. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.9

    (1). When $A$ is normal, the set $W(A)$ is the convex hull of the eigenvalues of $A$. For nonnormal ...

  10. IOS 多线程 NSOperation GCD

    1.NSInvocationOperation NSInvocationOperation * op; NSOperationQueue * que = [[NSOperationQueuealloc ...