Spring Bean Initialization callbacks and Destruction callbacks With Example


To interact with the container’s management of the bean lifecycle, you can implement the Spring InitializingBean and DisposableBean interfaces. The container calls afterPropertiesSet() for the former and destroy() for the latter to allow the bean to perform certain actions upon initialization and destruction of your beans.

Initialization callbacks

The org.springframework.beans.factory.InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container. The InitializingBean interface specifies a single method:

void afterPropertiesSet() throws Exception;


Destruction callbacks

implementing the org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the container containing it is destroyed. The DisposableBean interface specifies a single method:

void destroy() throws Exception;

Let's See An Example For Spring bean Initialization Callbacks and Destruction Call Backs


Tools Used In This Article

  • Spring 4.3.2
  • Maven 4.0.0
  • JDK 1.7

Project Directory Structure




Maven Dependencies

Add The Following Dependencies to the pom.xml
  • spring-core
  • spring-beans
  • spring-context

1.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.practice</groupId>
	<artifactId>afterProprtyDemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<dependencies>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.2.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.3.2.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.2.RELEASE</version>
		</dependency>


	</dependencies>
</project>

2.Writing Spring Beans

In This Example Im Writing one Spring Bean Named With WelcomeBean

WelcomeBean.java

package com.practice;


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

public class WelcomeBean implements InitializingBean, DisposableBean {

	public void destroy() throws Exception {
		System.out.println("destroy method");

	}

	public void afterPropertiesSet() throws Exception {
		 System.out.println("init method");

	}
	

}

2.Setting Up Spring Configuration File

applicationContext.xml

		<beans 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-4.3.xsd">
		<bean id = "demoBean" class = "com.practice.WelcomeBean" >
		
				</bean>
		
		</beans>

3.Now Create Test class ,RunThe Code

AppTest.java

package com.practice.test;

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

import com.practice.WelcomeBean;

public class AppTest {

	
	public static void main(String[] args) {
		ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		WelcomeBean account  = (WelcomeBean)context.getBean("demoBean");
		
		
		context.close();
	 


	}

}

output:

INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
init method
Sep 03, 2016 1:05:52 AM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1ba3c1f: startup date [Sat Sep 03 01:05:51 IST 2016]; root of context hierarchy
destroy method