public class Instrumentalist implements Performer, InitializingBean, DisposableBean {
private Instrument instrument;
private String song;
public void setInstrument(Instrument instrument)
{
this.instrument=instrument;
}
public void setSong(String song)
{
this.song=song;
}
public void afterPropertiesSet() throws Exception
{
System.out.println("Before Playing Instrument");
}
public void destroy() throws Exception
{
System.out.println("After Playing Instrument");
}
public void perform() {
// TODO Auto-generated method stub
System.out.println("Playing "+ song + " : ");
instrument.play();
}
}
在上面的例子中,只有我調出了afterPropertiesSet()
而不是銷燬方法。下面是我3210爲什麼在銷燬方法不會在下面自動調用時,對象會毀滅?
<bean id="dhiraj" class="Instrumentalist">
<property name="song" value="Sa Re Ga Ma" />
<property name="instrument" ref="piano" />
</bean>
<bean id="piano" class="Piano" />
,我從下面我main
方法稱爲 -
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-config.xml");
Performer performer1=(Performer)context.getBean("dhiraj");
performer1.perform();
你何時期待'摧毀()'被稱爲? – skaffman
感謝您的回覆。根據我的知識,當bean名稱「dhiraj」被實例化時,應調用afterPropertiesSet(),並在銷燬bean dhiraj之前調用destroy()方法。第一種方法是正確調用,但不是第二種。 –
可能的重複[何時是一個春天豆摧毀方法叫?](http://stackoverflow.com/questions/4460384/when-is-a-spring-beans-destroy-method-called) –