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

Disposable Bean 

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 DisposableBean interface which allows us to perform pre processing tasks when the container containing this bean is destroyed.

DisposableBean.java  
package org.springframework.beans.factory;

public abstract interface DisposableBean
{
  public abstract void destroy() throws Exception;
}
File Name  :  
/SpringCore001/conf/callback/destroy.xml 

File Name  :  
com/bethecoder/tutorials/spring3/basic/SimpleDisposableBean.java 
   
package com.bethecoder.tutorials.spring3.basic;

import org.springframework.beans.factory.DisposableBean;

public class SimpleDisposableBean implements DisposableBean {

  private String simpleProp;

  public String getSimpleProp() {
    return simpleProp;
  }

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

  @Override
  public void destroy() throws Exception {

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

File Name  :  
com/bethecoder/tutorials/spring3/tests/callbacks/DestructionCallback.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.spring3.tests.callbacks;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

public class DestructionCallback {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    System.out.println("Initializing ApplicationContext");
    ApplicationContext factory = new ClassPathXmlApplicationContext("destroy.xml");
    System.out.println("ApplicationContext Initialized");
    
    System.out.println("Accessing first bean");
    SimpleDisposableBean first = (SimpleDisposableBeanfactory.getBean("first");
    System.out.println(first);

    System.out.println("Accessing second bean");
    SimpleDisposableBean second = (SimpleDisposableBeanfactory.getBean("second");
    System.out.println(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;
    abc.registerShutdownHook();
  
  }

}
   

It gives the following output,
Initializing ApplicationContext
ApplicationContext Initialized

Accessing first bean
SimpleDisposableBean[FIRST]

Accessing second bean
SimpleDisposableBean[SECOND]

In destroy callback : SECOND
In destroy callback : null

In destroy callback : FIRST
In destroy callback : null


                    Print

   



Books Recommendation

 
 
bl  br