3
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();
   }

}

In above example only i got the out put in which afterPropertiesSet() is called but not destroy method. Below is my config.xml

<bean id="dhiraj" class="Instrumentalist">
    <property name="song" value="Sa Re Ga Ma" />
    <property name="instrument" ref="piano" />
</bean>

<bean id="piano" class="Piano" />

and i called from my main method as below -

ApplicationContext context = new ClassPathXmlApplicationContext("Spring-config.xml");
Performer performer1=(Performer)context.getBean("dhiraj");
performer1.perform();
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 1
    When were you expecting `destroy()` to be called? – skaffman Jul 15 '11 at 06:37
  • Thanks for your replay. As per my knowledge when bean name "dhiraj" is instantiated then afterPropertiesSet() should called and before destroying the bean dhiraj it should call destroy() method. The first method is calling properly but not second one. – Dhirendra Kumar Jul 15 '11 at 06:47
  • 2
    possible duplicate of [when is a spring beans destroy-method called?](http://stackoverflow.com/questions/4460384/when-is-a-spring-beans-destroy-method-called) – Ciro Santilli OurBigBook.com Mar 30 '15 at 11:52

4 Answers4

8

Try this:

AbstractApplicationContext context = new ClassPathXmlApplicationContext("Spring-config.xml");
//...
context.close();    //!!!

You have to close the context manually, otherwise Spring does not know that the bean is no longer needed and should be destroyed. Note that you have to use AbstractApplicationContext type as ApplicationContext interface does not define close().

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
4

For singleton beans like dhiraj, the destroy() lifecycle method will be called when, and only when, the application context is shut down.

If your code fragment is the entirety of your program, then destroy() will not be called because you're not closing the context properly.

Add context.close() to the end of your fragment, and you'll see destroy() being called.

skaffman
  • 398,947
  • 96
  • 818
  • 769
2

You need to close Context Object,Then Only destroy method is called.Check for img

ConfigurableApplicationContext Context= new ClassPathXmlApplicationContext("ApplicationContext.xml");
//.............
//.........
Context.close();

**

SAN
  • 107
  • 7
1

You can also register shutdown hook this way:

AbstractApplicationContext context = new ClassPathXmlApplicationContext("Spring-config.xml"); context.registerShutdownHook();

Damian Jeżewski
  • 1,246
  • 2
  • 14
  • 21