Spring 从零開始-03
2016-04-05 15:55
190 查看
这里说说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的样例
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
相关文章推荐
- jsp中应用JavaBean
- MyEclipse 使用 User Library 不自动部署到 WEB-INF/lib 文件夹的解决方案
- [j2ee]java中的xml操作
- 关于使用容联demo出现java.lang.NoClassDefFoundError
- Java NIO系列教程(十) Java NIO DatagramChannel
- Spring Mvc问题之session存取出现错误问题
- [java-solr日常一]solr4.9同步mysql数据
- Java字符编码
- Intellij:java web找不到WEB-INF下的文件
- Java类的初始化顺序
- Java-ASM
- Java反射机制
- 在Eclipse中使用JUnit4进行单元测试(高级篇)
- 深入理解java多态性
- ssh整合中出现的Caused by: org.springframework.beans.NotWritablePropertyException:
- 在Eclipse中使用JUnit4进行单元测试(中级篇)
- Java NIO系列教程(九) ServerSocketChannel
- 在Eclipse中使用JUnit4进行单元测试(初级篇)
- IDEA导入eclipse项目
- springmvc url-pattern配置/*报错