@Cesar
2015-12-27T13:11:33.000000Z
字数 1518
阅读 1824
spring 学习
xml:
<bean id="duke" class="cesar.test.spring.bean.Judgler"><constructor-arg value="15"></constructor-arg></bean>
java:
public class Judgler implements Performer{private int beanBags = 3;public Judgler(){}Judgler(int beanBags){this.beanBags = beanBags;}@Overridepublic void perform() {System.out.println("JUGGLING "+ beanBags+" BEANBAGS");}}
java文件:
package cesar.test.spring.bean;import cesar.test.spring.Instrument;public class Instrumentalist implements Performer {public Instrumentalist() {}private String song;private Instrument instrument;public void setSong(String song) {this.song = song;}public String getSong() {return song;}@Overridepublic void perform() {System.out.println("playing"+song+":");instrument.play();}public Instrument getInstrument() {return instrument;}public void setInstrument(Instrument instrument) {this.instrument = instrument;}}
2.1 注入简单值
<bean id="instru" class="cesar.test.spring.bean.Instrumentalist"><property name="song" value="我恨我爱你"></property></bean>
2.2 注入其他bean
<bean id="instru" class="cesar.test.spring.bean.Instrumentalist"><property name="song" value="我恨我爱你"></property><property name="instrument" ref=""></property></bean>
xml:
<bean id="theStage" class="cesar.test.spring.bean.Stage"factory-method="getInstance"></bean>
java:
public class Stage {private Stage(){}private static class StageSingletonHolder{static Stage instance = new Stage();}public static Stage getInstance(){return StageSingletonHolder.instance;}public void sayHello() {System.out.println("Hello");}}