title: When does bean's destroy-method will be called in spring?
body: <p>This is the bean class it is used to store the details about the particular user. It has two method named init and destroy. which needs to be called when bean is initilized and destroyed.</p>
  
  <pre><code>package org.springframework.HelloWorld.beans;
  
  public class User {
      private int userId;
      private String userName;
      private String address;
      private String mobileNumber;
  
      public User() {
          super();
      }
  
      public User(int userId, String userName, String address, String mobileNumber) {
          super();
          this.userId = userId;
          this.userName = userName;
          this.address = address;
          this.mobileNumber = mobileNumber;
      }
  
      public int getUserId() {
          return userId;
      }
  
      public void setUserId(int userId) {
          this.userId = userId;
      }
  
      public String getUserName() {
          return userName;
      }
  
      public void setUserName(String userName) {
          this.userName = userName;
      }
  
      public String getAddress() {
          return address;
      }
  
      public void setAddress(String address) {
          this.address = address;
      }
  
      public String getMobileNumber() {
          return mobileNumber;
      }
  
      public void setMobileNumber(String mobileNumber) {
          this.mobileNumber = mobileNumber;
      }
  
      @Override
      public String toString() {
          return "User [userId=" + userId + ", userName=" + userName
                  + ", address=" + address + ", mobileNumber=" + mobileNumber
                  + "]";
      }
  
      public void init(){
          System.out.println("Initialized");
      }
  
      public void destroy(){
          System.out.println("Destroyed");
      }
  }
  </code></pre>
  
  <p>This is the main method i have used. here i'm getting the User object from Beans.xml file.</p>
  
  <pre><code>public static void main(String[] args) {
          @SuppressWarnings("resource")
          ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml");
          User user = (User) ctx.getBean("user");
          System.out.println(user);
          user.setUserName("Gnanadurai Asudoss");
          user.setAddress("Chxxxxx");
          System.out.println(user);
          user = (User) ctx.getBean("user");
          System.out.println(user);
          user=null;
      }
  </code></pre>
  
  <p>Below is the Beans.xml file here i have mentioned bean, init method and destroy method. 
  <div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;bean id="user" class="org.springframework.HelloWorld.beans.User" init-method="init" destroy-method="destroy"&gt;
    &lt;property name="userId" value="1"&gt;&lt;/property&gt;
    &lt;property name="userName" value="Gnanadurai"&gt;&lt;/property&gt;
    &lt;property name="address" value="Saxxxx"&gt;&lt;/property&gt;
    &lt;property name="mobileNumber" value="xxxxxxxx49"&gt;&lt;/property&gt;
   &lt;/bean&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>This is the output i'm getting. Here you can see Initialized method is beeing called and Destroy method is not called.</p>
  
  <pre><code>Initialized
  User [userId=1, userName=Gnanadurai, address=Saxxx, mobileNumber=xxxxxxxx49]
  User [userId=1, userName=Gnanadurai Asudoss, address=Chxxxx, mobileNumber=xxxxxxxx49]
  User [userId=17505, userName=Gnanadurai Asudoss, address=Chxxxx, mobileNumber=xxxxxxxx49]
  </code></pre>
  
  <p>So I dont know when destroy method will be called.. Could you tell me when it will be called. Give me an example. </p>
  
  <p>Thanks...</p>