Share on Google+Share on Google+

DisposableBean Interface and InitializingBean in Spring

In this tutorial you will learn about Disposable Bean Interface and Initializing Bean of Spring framework.

DisposableBean Interface and InitializingBean in Spring

The InitializingBean and DisposableBean are two marker interfaces which call the  afterPropertiesSet() for the begining and destroy() for the last action of initialization and    destruction to be performed.


import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class StudentService implements InitializingBean, DisposableBean {
	String message;

	public String getMessage() {
		return message;

	public void setMessage(String message) {
		this.message = message;

	public void afterPropertiesSet() throws Exception {
		System.out.println(" After properties has been set : " + message);

	public void destroy() throws Exception {
		System.out.println("Cleaned everything!!");


package com.roseindia.common;

import org.springframework.context.ConfigurableApplicationContext;


public class AppMain {
	public static void main(String[] args) {
		ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "context.xml" });
		StudentService stud = (StudentService) context


<beans xmlns=""

  <bean id="studentService" class="">
    <property name="message" value="property message" />


When you run this application it will display message as shown below:

After properties has been set : property message
[email protected]
Cleaned everything!!

Download this example code



Posted on: September 3, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null