Searching...
Saturday, 15 June 2013

Spring Bean life cycle callback methods

10:15

 

There are two ways we can implement initialization/destruction call back methods in Spring Bean

1.Implementing Initialization/destruction interface

The org.springframework.beans.factory.InitializingBean interface specifies a single method
void afterPropertiesSet() throws Exception;

The org.springframework.beans.factory.DisposableBean interface specifies a single method
void destroy() throws Exception;

2. Xml based configuration

add init/destroy attribute in bean tag
init-method="init


In the below example, the call back methods are applied for Employee (XML Based) and Customer beans (Implements interfaces)

1. Create java beans


package com.vinod.test;

public class Employee {
    private String name;
    private String address;

    public String getName() {
        return name;
    }

//Getters and setters
    public void init() {
        System.out.println("Bean initializing");
    }

    public void destroy() {
        System.out.println("Bean destroying");
    }
}
 


package com.vinod.test;

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

public class Customer implements InitializingBean, DisposableBean {
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public void destroy() throws Exception {
        System.out.println("Bean destroying");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Bean starting");
    }

}
 


2. Create spring configuration

<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-2.5.xsd">

   
    <bean id="employeeBean" class="com.vinod.test.Employee"
        init-method="init" destroy-method="destroy">
    </bean>
       
    <bean id="customer" class="com.vinod.test.Customer">
    </bean>
   
</beans>


3. Create a Test class


package com.vinod.test;

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

public class SpringInitializeTest {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-core-application-context.xml");
       
        //Employee bean
        Employee obj = (Employee) context.getBean("employeeBean");
        obj.setName("vinod-->Employee");
        System.out.println(obj.getName());
       
        //Customer bean
       
        Customer customer = (Customer) context.getBean("customer");
        customer.setName("vinod-->Customer");
        System.out.println(customer.getName());
    }

}
  

4. Output

Bean initializing

Bean starting

vinod-->Employee

vinod-->Customer 

0 comments: