Spring 从零开始-03
2015-07-04 14:43
696 查看
这里说说bean装配集合,spring的支持的集合元素,其基本使用方式如同与Java的集合,所以如果对Java的集合不太了解的可以先找个帖子好好学习一下, 时间关系这里就不说了。~~
list的例子
set和list使用方式一致,把list换成set就好了,主要是set中元素不能重复。另外多说一点,为了解耦Spring推荐使用基于接口的编程方式。另外数组也是使用这种装配方式。
map
properties装配,对比与map,properties的key和value只能是string类型,其余都差不多
好了,差不多了,上代码吧。
http://download.csdn.net/detail/wsrspirit/8868347
list的例子
[code]public interface Instrument { void play(); }
[code]public class Guitar implements Instrument { @Override public void play() { // TODO Auto-generated method stub System.out.println("dang dang dang"); } }
[code]public class Piano implements Instrument{ @Override public void play() { // TODO Auto-generated method stub System.out.println("ding ding ding"); } }
[code]public class Saxophone implements Instrument{ @Override public void play() { // TODO Auto-generated method stub System.out.println("wu wu wu "); } }
[code]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(); } }
[code]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(); } }
[code]<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
[code]<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>
[code]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(); } } }
[code]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
相关文章推荐
- java 泛型
- java教程网
- Eclipse 中的GitHub插件
- Java彻底 - WEB容器的侦听具体解释 ServletContextListener
- 毫秒转换 成日期 相互转换
- java正则表达式大全
- java图片高质量压缩
- spring组件<context:component-scan>详解
- 使用Eclipse-Maven-git做Java开发(3)--Eclipse的安装和配置
- JAVA解析逻辑表达
- JAVA JSON 开发记录
- 2015070407 - EffactiveJava笔记 - 第59条 避免不必要地使用受检异常
- java 中的 aes 加密算法
- JAVA中使用FTPClient上传下载
- 学会JAVA可变长度的参数(Varargs)
- Java并发的四种风味:Thread、Executor、ForkJoin和Actor
- java总结(之八)--并发
- Java编程思想学习笔记——访问权限修饰词
- 2015070406 - EffactiveJava笔记 - 第58条 异常使用原则
- 将Eclipse中的项目迁移到Android Studio中的方法