Spring进阶学习一
来源:互联网 发布:怎么判断端口是否连通 编辑:程序博客网 时间:2024/05/17 23:02
Spring配置多个属性值,或者spring装配list、array、set、map等方式时可以使用如下方式,比如,这里有个人会演奏很多的乐器,那么需要有乐器Instrument这个接口,然后定义具体的乐器去实现这个乐器接口,还有一个表演者Performer这个接口,因为不只是只有这一个表演者可能。下面具体讲解
(1)建立表演者接口Performer
package com.springaction.P30;public interface Performer {void perform() ;}(2)建立Instrument这个接口
package com.springaction.P40;public interface Instrument {public void play();}
(3)建立OneManHand(一个多才多艺的表演者)这个类,该类要实现Performer,并且内部维护了乐器Instrument这个对象,一般内部定义个全局属性,都会有相应的set方法,以便xml文件中进行IOC
package com.springaction.P46;import java.util.Collection;import java.util.Map;import com.springaction.P30.Performer;import com.springaction.P40.Instrument;public class OneManBand implements Performer{//对list进行测试 /*private Collection<Instrument> instruments;public void setInstruments(Collection<Instrument> instruments) {this.instruments = instruments;}@Overridepublic void perform() {for(Instrument instrument:instruments) {instrument.play();}}*/private Map<String,Instrument> instruments;public void setInstruments(Map<String,Instrument> instruments) {this.instruments = instruments;}@Overridepublic void perform() {for(String key:instruments.keySet()) {System.out.println(key+" :");Instrument instrument = instruments.get(key);instrument.play();}}}(4)编辑Spring的xml文件,将上面的类OneManHand注入到bean中
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="sonnet29" class="com.springaction.P34.Sonnet29"></bean><bean id="duck" class="com.springaction.P30.PoeticJuggler"> <constructor-arg value="15"></constructor-arg> <constructor-arg ref="sonnet29"></constructor-arg> <!-- <constructor-arg value="小名"></constructor-arg>--></bean><bean id="kenny" class="com.springaction.P40.Instrumentalist"> <property name="song" value="我爱你中国"></property> <property name="instrument" ref="saxophone"></property></bean><bean id="saxophone" class="com.springaction.P40.Saxophone"></bean><!-- List测试代码部分<bean id="hank" class="com.springaction.P46.OneManBand"> <property name="instruments"> <list> <ref bean="guitar"/> <ref bean="cymbal"/> <ref bean="hormonica"/> </list> </property></bean>--><bean id="hank" class="com.springaction.P46.OneManBand"> <property name="instruments"> <map> <entry key="GUITAR" value-ref="saxophone"></entry> <entry key="CYMBAL" value-ref="saxophone"></entry> <entry key="HARMONICA" value-ref="saxophone"></entry> </map> </property></bean></beans>(5)测试代码部分
package com.springaction.P30;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class PerformerTest {@Testpublic void testPerform() {ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");Performer performer = (Performer)ctx.getBean("duck");Performer performer2 = (Performer)ctx.getBean("hank");performer.perform();performer2.perform();}}(6)结果展示:
GUITAR :Ding Ding DingCYMBAL :Ding Ding DingHARMONICA :Ding Ding Ding
0 0
- Spring进阶学习一
- Spring进阶(一)
- Lua学习进阶一
- Android_Fragment 学习进阶(一)
- jaee学习进阶一
- 数据库学习进阶一
- Spring进阶一:Spring开发环境搭建
- 8.5 js进阶学习一
- Fragment学习进阶<一>-------静态
- python学习进阶(一)
- Hibernate进阶学习(一)
- JavaScript进阶学习(一)
- java web学习(进阶篇)一 从了解Spring开始
- Struts + Spring + Hibernate 进阶开端(一)
- Spring Boot进阶之Web进阶 学习笔记
- Spring学习(一) spring简介
- Spring学习(一):Spring是什么
- struts 2学习进阶 初识struts2(一)
- 在 Transact-SQL 中使用 TRY...CATCH XACT_STATE
- JBoss as 配置
- 数据结构笔记——第五章 树和二叉树
- Allegro进行PCB级的信号完整性仿真
- 理解Thread.interrupt()的含义
- Spring进阶学习一
- 单双引号
- C++创建对象的两种方法
- Android SDK Manager国内下载缓慢的问题
- Android NDK 环境搭建
- Ubuntu下外接显示器双屏显示的方法
- cocos2dx编译android注意事项
- php5+Apache2+mysql+Eclipse debug完全配置(略有补充)
- mysql何时使用myisam, 何时使用innodb ?