tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > Core > Destruction Callback

Destruction Callback 

Spring Inversion of Control (IoC) also known as Dependency Injection (DI) is a process by which objects define their dependencies with collaborating objects. Spring provides a better alternative to using DisposableBean interface. The attribute destroy-method allows us to specify a no-argument method as a destruction callback method.

File Name  :  

File Name  :  
package com.bethecoder.tutorials.spring3.basic;

public class SimpleDisposableBean2 {

  private String simpleProp;

  public String getSimpleProp() {
    return simpleProp;

  public void setSimpleProp(String simpleProp) {
    this.simpleProp = simpleProp;

  public void dispose() {

    System.out.println("In custom destroy callback : " + simpleProp);
    simpleProp = null
    System.out.println("In custom destroy callback : " + simpleProp);
  public String toString() {
    return "SimpleDisposableBean2[" + simpleProp + "]";

File Name  :  
Author  :  Sudhakar KV
Email  :  [email protected]
package com.bethecoder.tutorials.spring3.tests.callbacks;

import org.springframework.context.ApplicationContext;

import com.bethecoder.tutorials.spring3.basic.SimpleDisposableBean2;

public class DestructionCallback2 {

   @param args
  public static void main(String[] args) {
    System.out.println("Initializing ApplicationContext");
    ApplicationContext factory = new ClassPathXmlApplicationContext("destroy2.xml");
    System.out.println("ApplicationContext Initialized");
    System.out.println("Accessing first bean");
    SimpleDisposableBean2 first = (SimpleDisposableBean2factory.getBean("first");

    System.out.println("Accessing second bean");
    SimpleDisposableBean2 second = (SimpleDisposableBean2factory.getBean("second");
     * Register a Shutdown hook for graceful Shutdown.
     * Calls the relevant destroy methods on your singleton beans 
     * so that all resources are released
    AbstractApplicationContext abc = (AbstractApplicationContext factory;


It gives the following output,
Initializing ApplicationContext
ApplicationContext Initialized

Accessing first bean

Accessing second bean

In custom destroy callback : SECOND
In custom destroy callback : null

In custom destroy callback : FIRST
In custom destroy callback : null


bl  br