title: How to destroy only a single bean in Spring?
body: <p>I have a lot of singleton beans defined in my spring application context both different objects of the same type and objects of different types.
  To do some pre-destroy operations, I have implemented the <code>DisposableBean</code> interface for the bean classes. But, I would like to know how to destroy a bean with a particular id.</p>
  
  <p>Here is my <code>destroybean.xml</code></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="st" class="spring17.Student"&gt;
              &lt;property name="sno" value="101"/&gt;
              &lt;property name="sname" value="Smith"/&gt;
              &lt;property name="age" value="20"/&gt;
          &lt;/bean&gt;
  
          &lt;bean id="st1" class="spring17.Student"&gt;
              &lt;property name="sno" value="102"/&gt;
              &lt;property name="sname" value="Scott"/&gt;
              &lt;property name="age" value="22"/&gt;
          &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Here is my main class</p>
  
  <pre><code>package spring17;
  
  import org.springframework.context.support.GenericXmlApplicationContext;
  
  public class SpringPrg {
      @SuppressWarnings("resource")
      public static void main(String args[])
      {
          GenericXmlApplicationContext gc=new GenericXmlApplicationContext("classpath:destroybean.xml");
  
          Student st=gc.getBean("st",Student.class);
  
          System.out.println(st);
  
          gc.destroy();
      }
  }
  </code></pre>
  
  <p>When i call the <code>gc.destroy()</code> <code>st1</code> is also being destroyed which I don't want. You may suggest adding <code>lazy-init</code> attribute for <code>st1</code> but it is not what I wanted.</p>
  
  <p>Thanks in advance.</p>