Home Tutorial Spring Spring3 Ioc DisposableBean Interface and InitializingBean in Spring


Share on Google+Share on Google+
DisposableBean Interface and InitializingBean in Spring
Posted on: September 3, 2010 at 12:00 AM
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.


package com.roseindia.student.services;

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;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.roseindia.student.services.StudentService;

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


<beans xmlns="http://www.springframework.org/schema/beans"

  <bean id="studentService" class="com.roseindia.student.services.StudentService">
    <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
Cleaned everything!!

Download this example code


Related Tags for DisposableBean Interface and InitializingBean in Spring:

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.

Posted on: September 3, 2010

Recommend the tutorial

Advertisements Advertisements