title: Spring bean destroy-method , singleton and prototype scopes
body: <p>I am new to the spring framework, started with some tutorials to learn it.</p>
  
  <p>I have following files,</p>
  
  <p># MainProgram.java</p>
  
  <pre><code>package test.spring;
  import org.springframework.context.support.AbstractApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  public class MainProgram {
          public static void main(String[] args) {
                AbstractApplicationContext context = 
                                new ClassPathXmlApplicationContext("Bean.xml");     
                HelloSpring obj = (HelloSpring) context.getBean("helloSpring");
                obj.setMessage("My message");
                obj.getMessage();
                context.registerShutdownHook();
  
          }
   }
  </code></pre>
  
  <p># HelloSpring.java</p>
  
  <pre><code>package test.spring;
  
  public class HelloSpring   {
       private String message;
  
       public void setMessage(String message){
        this.message  = message;
        System.out.println("Inside setMessage");
     }
  
     public void getMessage(){
        System.out.println("Your Message : " + this.message);
     }
  
     public void xmlInit() {
      System.out.println("xml configured  initialize");
     } 
  
      public void xmlDestroy() {
      System.out.println("xml configured destroy");
      }
  
    }
  </code></pre>
  
  <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-3.0.xsd"&gt;
  
       &lt;bean id="helloSpring" class="test.spring.HelloSpring" 
            scope="prototype" init-method="xmlInit" destroy-method="xmlDestroy"&gt;
  
       &lt;/bean&gt;
       &lt;/beans&gt;
  </code></pre>
  
  <p>When I take <code>scope="singleton"</code>  my output is :</p>
  
  <pre><code> xml configured  initialize
   Inside setMessage
   Your Message : My message
   xml configured destroy
  </code></pre>
  
  <p>When I take <code>scope="prototype"</code>  my output is :</p>
  
  <pre><code> xml configured  initialize
   Inside setMessage
   Your Message : My message
  </code></pre>
  
  <p><code>xmlDestroy()</code> method is called with <code>singleton</code> scope bean but not with <code>prototype</code>
  kindly help me for the following ,</p>
  
  <p>Is this correct? if so, what would be possible reasons?</p>
  
  <p>Also I have some queries like,</p>
  
  <p>what is difference or relation between 
  <code>ApplicationContext , AbstractApplicationContext and ClassPathXmlApplicationContext</code></p>