In Spring, InitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to perform certain actions upon bean initialization and destruction.
  1. For bean implemented InitializingBean, it will run afterPropertiesSet() after all bean properties have been set.
  2. For bean implemented DisposableBean, it will run destroy() after Spring container is released the bean.

Example

Here’s an example to show you how to use InitializingBeanand DisposableBean. A CustomerService bean to implement both InitializingBean and DisposableBean interface, and has a message property.
package com.mkyong.customer.services;
 
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
 
public class CustomerService 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("Init method after properties are set : " + message);
 }
 
 public void destroy() throws Exception {
   System.out.println("Spring Container is destroy! Customer clean up");
 }
 
}
File : Spring-Customer.xml
 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-2.5.xsd">
 
        id="customerService" class="com.mkyong.customer.services.CustomerService">
   name="message" value="i'm property message" />