Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Bean CallBack Methods

The Spring Framework provides several marker interfaces to change the behavior of your bean in the container; they include InitializingBean and DisposableBean. Implementing these interfaces will result in the container calling afterPropertiesSet() for the former and destroy() for the latter to allow the bean to perform certain actions upon initialization and destruction.

The life cycle of a Spring bean is easy to understand. When a bean is instantiated, it may be required to perform some initialization to get it into a usable state. Similarly, when the bean is no longer required and is removed from the container, some cleanup may be required.

Beans can be notified after creation and all properties are set, and before they are destroyed and removed from the bean container. This involves specifying the callback method to be invoked by the container. This is done in XML by specifying attributes init-method="myinit", for the initialization callback, and destroy-method="mydestroy", for the destroy callback. "myinit" and "mydestroy" are names of instance methods in the bean class.

<bean id="abean" class="packg.classname" init-method="myinit">
......
</bean>
<bean id="abean" class="packg.classname" destroy-method="mydestroy">
......
</bean>

InitializingBean and DisposableBean callback interfaces

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;

This is not a preferrable way to initialize the bean because it tightly couple your bean class with spring container. A better approach is to use "init-method" attribute in bean definition in applicationContext.xml file.

Similarly, 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;

Example of Bean Auto-wiring byName

Employee.java
package callBackMethods;

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


public class Employee implements InitializingBean, DisposableBean
{
private String id;
private String name;

public String getId() {
return id;
}
public void setId(String id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}

public void preMethod(){
System.out.println("Init method called");
}

public void postMethod(){
System.out.println("Destroy method called");
}


@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Bean Initialize method called");
}
@Override
public void destroy() throws Exception {
System.out.println("Bean Destroy method called");
}
}

context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">

<bean id="employee" class="callBackMethods.Employee"
init-method="preMethod" destroy-method="postMethod" >
<property name="id" value="1"/>
<property name="name" value="Test"/>
</bean>


</beans>

Main.class

package callBackMethods;

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

public class Main {

public static void main(String[] args) {

AbstractApplicationContext context =
new ClassPathXmlApplicationContext( "callBackMethods/context.xml");

Employee emp = context.getBean("employee",Employee.class);
System.out.println(emp.getId()+" "+emp.getName());
context.destroy();
}
}

     << Previous
Next >>