title: Destroy method is not working in spring framework
body: <p>Edit:
  This question is not same as <a href="https://stackoverflow.com/questions/4460384/when-is-a-spring-beans-destroy-method-called">When does destroy method is called</a> because I am properly calling <code>context.registerShutdownHook</code> and my bean is getting destory as you can see from the logs. My problem is spring is not calling my method. I have checked this question before asking here.</p>
  
  <p>I am configuring the graceful destroy in my application using spring framework. When I am running the program it is not calling the destory method specified in the bean.xml. Please help me what am I doing wrong. </p>
  
  <p>here is <a href="http://sscce.org" rel="noreferrer">SSCCE</a></p>
  
  <p>Bean.xml </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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.xsd"&gt;
  
      &lt;bean id="helloworld" class="com.hello.pojo.HelloWorld" 
            scope="prototype" init-method="init" destroy-method="destroy"&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>HelloWord.java</p>
  
  <pre><code>package com.hello.pojo;
  
  public class HelloWorld {
  
  
      private String message;
  
      public String getMessage() {
          return message;
      }
  
      public void setMessage(String message) {
          this.message = message;
      }
  
      public void init(){
          System.out.println("Bean initializating is in progress");
      }
  
      public void printMessage(){
          System.out.println("Your message: "+getMessage());
      }
      public void destroy(){
          System.out.println("Bean is being destroyed");
      }
  
  }
  </code></pre>
  
  <p>MainApp.java</p>
  
  <pre><code>package com.main;
  
  import org.springframework.context.support.AbstractApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  import com.hello.pojo.HelloWorld;
  
  
  public class MainApp {
  
      public static void main(String[]args){
          AbstractApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
          HelloWorld objA = (HelloWorld) context.getBean("helloworld");
          objA.setMessage("I am Object A");
          objA.printMessage();
          context.registerShutdownHook();
      }
  
  }
  </code></pre>
  
  <p>Output</p>
  
  <pre><code>May 27, 2013 11:59:14 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e9028874: startup date [Mon May 27 23:59:14 EDT 2013]; root of context hierarchy
  May 27, 2013 11:59:14 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  INFO: Loading XML bean definitions from class path resource [Bean.xml]
  Bean initializating is in progress
  Your message: I am Object A
  May 27, 2013 11:59:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@63390b47: defining beans [helloworld]; root of factory hierarchy
  May 27, 2013 11:59:14 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
  INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@e9028874: startup date [Mon May 27 23:59:14 EDT 2013]; root of context hierarchy
  May 27, 2013 11:59:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons
  INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@63390b47: defining beans [helloworld]; root of factory hierarchy
  </code></pre>
  
  <p>Amendment:
  I have tried <code>close</code> and <code>registerShutdownHook()</code> to close the context and none of them works.</p>