Bean Life Cycle

To control the spring bean life cycle spring has given two interfaces. They are:
  • Initializing bean
  • Disposable bean
These interfaces are available in org.springframework.beans.factory package. Any spring bean can provide the implementation of these two interfaces.
Whenever we create the spring container object the following steps are carried out by the container.
  1. Spring bean object is created
  2. Dependency is established
  3. Check whether afterPropertiesSet() method is available or not. If it is available execute it.
  4. At the time of removal of object, check if destroy method is available. If it is available then execute it.
First create a Address bean under src as shown below. File: com.ram.beans.Address.java
package com.ram.beans;

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

public class Address implements InitializingBean, DisposableBean{
 private String street;
 private String city;
 private String state;
 
 public String getStreet() {
  return street;
 }
 public void setStreet(String street) {
  this.street = street;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
 public String getState() {
  return state;
 }
 public void setState(String state) {
  this.state = state;
 }
 
 @Override
 public void destroy() throws Exception {
  System.out.println("destroy() method called ...");
 }
 @Override
 public void afterPropertiesSet() throws Exception {
  System.out.println("afterPropertiesSet() method called ...");
 }
}


Next create applicationContext.xml file under src. The file is given below.
<?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:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
 <bean id="addr" class="com.ram.beans.Address">
  <property name="street" value="Richmond"></property>
  <property name="city" value="Melbourne"></property>
  <property name="state" value="Victoria"></property>
 </bean>
 
</beans>
Next create a main class under src as shown. File: com.ram.app.BeanLifeCycle.java
package com.ram.app;

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

import com.ram.beans.Address;

public class BeanLifeCycle {
 public static void main(String[] args) {
  AbstractApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");
  Address address = container.getBean("address", Address.class);
  
  System.out.println("Street = "+address.getStreet());
  System.out.println("City = "+address.getCity());
  System.out.println("State = "+address.getState());
  
  // To remove spring bean objects available in container
  container.registerShutdownHook();
 }
}

Execute BeanLifeCycle.java file. Output is given below.
Instead of using InitializingBean and DisposableBean we can use our own methods as the life cycle methods. For Example:
public class Address{
 public void init() throws Exception{
  System.out.println("init() method called ...");
 }
 
 public void destroy() throws Exception{
  System.out.println("destroy() method called ...");
 }
}
If the spring container wants to call these methods, we have to configure in the spring configuration file as shown below:
<bean id="addr" class="com.ram.beans.Address" init-method="init" destroy-method="destroy">

No comments:

Post a Comment