这里说说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. mappingResources,annotatedClasses(映射)

    这两个是有本质区别的,光看名字也能看出来,哈哈,好了,入正题: mappingResources用于指定少量的hibernate配置文件像这样 Xml代码 1 2 3 4 5 6 7 <prop ...

  2. Java [Leetcode 283]Move Zeroes

    题目描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  3. 常用高度——获取浏览器窗口的高度(jquery和js)

    一:针对浏览器的常用高度 jquery的用法: <script type="text/javascript"> $(document).ready(function() ...

  4. Spring AOP--返回通知,异常通知和环绕通知

    在上篇文章中学习了Spring AOP,并学习了前置通知和后置通知.地址为:http://www.cnblogs.com/dreamfree/p/4095858.html 在本文中,将继续上篇的学习, ...

  5. C# 随机读写入文件

    先来代码再解释 public Worker(string path) { FileStream fs = new FileStream( path, FileMode.OpenOrCreate, Fi ...

  6. ZOJ 3264 Present for MM

    寒假开始了···但是做题没有结束嘤··· 然后是dp专场嘤··· 题意:背包问题,给出背包容量和物品对数,每对物品都有特殊的关系:第一种关系是两个物品都取有价值,若只取一个则失去价值:第二种是两个物品 ...

  7. JMX-JAVA进程监控利器

    Java 管理扩展(Java Management Extension,JMX)是从jdk1.4开始的,但从1.5时才加到jdk里面,并把API放到java.lang.management包里面. 如 ...

  8. 学习xcode 博客

    csdn http://blog.csdn.net/tianyitianyi1/article/category/1160169/2

  9. c#: 解析json, 转成xml, 简单方便

    没看到.net framework中有这样的功能, 懒得到处找了, 索性花点时间自己写一个 /* * Created by SharpDevelop. * Date: 2013/6/24 * User ...

  10. inline和宏之间的区别

    1.内联函数在编译时展开,而宏在预编译时展开 2.在编译的时候,内联函数直接被嵌入到目标代码中去,而宏只是一个简单的文本替换. 3.内联函数可以进行诸如类型安全检查.语句是否正确等编译功能,宏不具有这 ...