本章类容来源于Spring in Action3第二章。
内容:
- 声明bean
- 构造器注入和setter方法注入
- 装配bean
- 控制bean的创建和销毁
观众朋友们好,这里是上海电视台,欢迎收看中国达人秀节目。
本章类容来源于Spring in Action3第二章。
内容:
public interface Performer { public void perform(); }所有参与中国达人秀的同学都要实现该接口。
<?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" > </beans>
public class Juggler implements Performer { private int beanBags = 3; public Juggler() { } public Juggler(int beanBags) { this.beanBags = beanBags; } public void perform() { System.out.println("juggler " + beanBags + " bags"); } }
<bean id="duke" class="in.action3.chapter2.Juggler"></bean>好了,我们使用下面的代码,给duke一个排练的机会
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/inject-service.xml"); Performer performer = (Performer)ctx.getBean("duke"); performer.perform(); }输出juggler 3 bags。
<bean id="duke" class="in.action3.chapter2.Juggler"> <constructor-arg value="15"></constructor-arg> </bean>如果不配置<constructor-arg/>,Spring使用默认的构造方法,配置了<constructor-arg/>,Spring将调用Juggler的另一个构造方法。
public class PoeticJuggler extends Juggler { private Poem poem ; public PoeticJuggler(Poem poem) { super(); this.poem = poem; } public PoeticJuggler(int bags, Poem poem) { super(bags); this.poem = poem; } @Override public void perform() { super.perform(); System.out.println("while reciting ...."); poem.recite(); } }PoeticJuggler持有Poem接口的引用,Poem接口定义:
public interface Poem { public void recite(); }我们定义一首诗Sonnet29,实现Poem接口:
public class Sonnet29 implements Poem { public void recite() { System.out.println("every body, now, I will recite Sonnet29, when ......"); } }在XML配置中,将Sonnet29声明为一个bean:
<bean id="sonnet29" class="in.action3.chapter2.Sonnet29"></bean>现在我们来声明一个PoeticJuggler的bean,讲sonnet29注入给它:
<bean id="poeticDuke" class="in.action3.chapter2.PoeticJuggler"> <constructor-arg value="15"></constructor-arg> <constructor-arg ref="sonnet29"></constructor-arg> </bean>poeticDuke排练下:
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/inject-service.xml"); Performer performer = (Performer)ctx.getBean("poeticDuke"); performer.perform(); }
public class Stage { private Stage(){}; private static class StageSingletonHolder{ static Stage instance = new Stage(); } public static Stage getInstance(){ return StageSingletonHolder.instance; } }题外话:
<bean id="stage" class="in.action3.chapter2.Stage" factory-method="getInstance"></bean>
<bean id="ticket" class="in.action3.chapter2.Ticket" scope="prototype"></bean>通过scope属性来指定bean的作用域。
<bean id="stage" class="in.action3.chapter2.Stage" factory-method="getInstance" init-method="turnOnLights" destroy-method="turnOffLights"></bean>如果上下文中有很多Bean拥有相同名字的初始化和销毁方法,可以使用 beans元素的default-init-method和default-destory-method属性。
<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" default-init-method="turnOnLights" default-destroy-method="turnOffLights" >
public class Instrumentalist implements Performer { private Instrument instrument; private String song; public Instrument getInstrument() { return instrument; } public void setInstrument(Instrument instrument) { this.instrument = instrument; } public String getSong() { return song; } public void setSong(String song) { this.song = song; } public void perform() { System.out.println("表演的歌曲名字是:"+song); instrument.play(); } }定义乐器接口:
public interface Instrument { public void play(); }定义萨克斯乐器:
public class Saxophone implements Instrument { public void play() { System.out.println("TOOT TOOT TOOT"); } }Spring 配置:
<bean id="saxophone" class="in.action3.chapter2.Saxophone"></bean> <bean id="kenney" class="in.action3.chapter2.Instrumentalist"> <!-- setter注入简单的值 --> <property name="song" value="Jinle bells"></property> <!-- setter注入bean --> <property name="instrument" ref="saxophone"></property> </bean>执行代码:
public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring/inject-service.xml"); Performer performer = (Performer)ctx.getBean("kenney"); performer.perform(); ctx.close(); }输出:
表演的歌曲名字是:Jinle bells TOOT TOOT TOOT
<bean id="kenney" class="in.action3.chapter2.Instrumentalist"> <!-- setter注入简单的值 --> <property name="song" value="Jinle bells"></property> <!-- setter注入bean --> <property name="instrument" > <!-- 注入内部Bean,该bean是kenney私有的 --> <bean class="in.action3.chapter2.Saxophone"></bean> </property> </bean>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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" >重新定义kenney bean配置
<bean id="saxophone" class="in.action3.chapter2.Saxophone"></bean> <bean id="kenney" class="in.action3.chapter2.Instrumentalist" p:song="Jinle bells" p:instrument-ref="saxophone"> </bean>使用<porperty>还是p取决于你, 他们是等价的,p的主要优点是简洁。
public class OneManBand implements Performer { private Collection<Instrument> instruments; public Collection<Instrument> getInstruments() { return instruments; } public void setInstruments(Collection<Instrument> instruments) { this.instruments = instruments; } public void perform() { for (Instrument instrument : instruments) { instrument.play(); } } }
<bean id="hank" class="in.action3.chapter2.OneManBand"> <property name="instruments"> <list> <ref bean="saxophone"/> <ref bean="guitar"/> <ref bean="harmonica"/> </list> </property> </bean>
public class OneManBand implements Performer { private Map<String, Instrument> instruments; public Map<String, Instrument> getInstruments() { return instruments; } public void setInstruments(Map<String, Instrument> instruments) { this.instruments = instruments; } public void perform() { for (Entry<String, Instrument> entry : instruments.entrySet()) { System.out.println("乐器:"+entry.getKey()+" 演奏:"); entry.getValue().play(); } } }配置:
<bean id="hank" class="in.action3.chapter2.OneManBand"> <property name="instruments"> <map> <entry key="saxophone" value-ref="saxophone"></entry> <entry key="guitar" value-ref="guitar"></entry> <entry key="harmonica" value-ref="harmonica"></entry> </map> </property> </bean>
public class OneManBand implements Performer { private Properties instruments; public Properties getInstruments() { return instruments; } public void setInstruments(Properties instruments) { this.instruments = instruments; } public void perform() { for (Entry<Object, Object> entry : instruments.entrySet()) { System.out.println("乐器:"+entry.getKey()+" 演奏:"+ entry.getValue()); } } }配置:
<bean id="hank" class="in.action3.chapter2.OneManBand"> <property name="instruments"> <props> <prop key="saxophone">saxophone</prop> <prop key="guitar">guitar</prop> <prop key="harmonica">harmonica</prop> </props> </property> </bean>
<property name="someNullProperty"><null/></property>