Ad

Thursday 26 March 2015

Spring bean Life Cycle - Callback Methods

In this post we will learn about the spring bean life cycle callback methods.Spring container provides the below two callback methods for the bean,so that the bean can perform certain actions during initialization or on destruction of the bean.
  • Initialization callback - After the bean is created by the container and the properties are set,the container will call this callback method.
  • Destruction callback - when the bean is about to destroyed i.e when the container is about to exit,the container will call this callback method.
In this post we will see how to configure this callback methods using the application context file (using attributes init-method and destroy-method).Below code example will explain this in detail.

Student - Spring bean class for which we will configure the callback methods.
CallbackDemo -  Main class which loads the container and initializes the bean.
Studentcontext.xml - Metadata configuration details for the spring container.

Student.java

package test;

public class Student {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void init() {
System.out.println("Bean initialization callback invoked for student::"
+ this.name);
}

public void destroy() {
System.out.println("Bean Destruction callback invoked for student::"
+ this.name);
}
}

studentcontext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<bean id="student" class="test.Student" init-method="init" destroy-method="destroy">
<property  name="name" value="Ram"/>
</bean>

</beans>

As we can see in the application context file,we used the below two attributes in the bean definition of student.
  • init-method - This will configure the initialization callback method(which is init) for the student bean
  • destroy-method - This will configure the destruction callback method(which is destroy) for the student bean
CallbackDemo.java

package test;

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

public class CallbackDemo {

public static void main(String[] args) {
AbstractApplicationContext  bf = new ClassPathXmlApplicationContext(
new String[] { "studentcontext.xml" });
bf.registerShutdownHook();
Student student= bf.getBean("student",Student.class);
}

}

We need to register the shutdownhook on the AbstractApplicationContext,so that the container will have the graceful shutdown and hence the corresponding destruction callback method on the bean will be called.

OUTPUT:

When we run the callbackDemo class, we will get the below output

Bean initialization callback invoked for student::Ram
Bean Destruction callback invoked for student::Ram

As we can see in the output,spring container
  • invoked the initialization callback(init method), after the properties in the bean are set.
  • invoked the destruction callback(destroy method) ,when the container is about to exit.

No comments:

Post a Comment