Recent Posts

Saturday, 8 June 2019

Bean Post Processor

BeanPostProcessor
     In Spring Framework, when we activate ApplicationContext container then ApplicationContext container will perform the following actions automatically without having developer's interaction.
1. ApplicatinContext will read beans definitions from beans configuration file.
2. ApplicationContext will recognize beans classes and creates objects for bean classes
3. ApplicationContext will perform initialization by executing initialization methods.
4. When IOCContainer is shutdown then bean objects are destroyed.
     In the above Bean lifecycle, if we want to provide customizations over beans initializations then we have to use a predefined interface provided by Spring framework like
"org.springframework.beans.factory.config.BeanPostProcessor".
BeanPostProcessor contains the following two methods
1. public void postProcessBeforeInitialization(Object bean, String name) throws BeansException
     This method will be executed just before performing bean initialization and immediately after bean instantiation, that is, before executing init() method if we provide custom initialization method init().
2. public void postProcessAfterInitialization(Object bean, String name) throws BeansException
     This method will be executed immediately after performing beans initialization, that is, after executing init() method if we provide custom initialization method init().
     To use BeanPostPrcessor in Spring applications we have to declare a user defined class as an implementation class to BeanPostProcessor interface, we must implement the above specified methods and we must configure implementation class in bean configuration file. If we provide like this then container will detect BeanPostProcessor implementation automatically and IOC Container will execute the BeanPostProcessor methods in the respective locations of the bean's lifecycle.
E.g.
package com.ashok.spring.core.bean.beanpostprocessor.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee  {
   public void init() {
      System.out.println("Employee Bean Object Initialization through init() method");
   }

   public void destroy() {
      System.out.println("Employee Object Destroying through destroy() method");
   }
}
package com.ashok.spring.core.bean.beanpostprocessor.impl;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import com.ashok.spring.core.bean.beanpostprocessor.beans.Employee;

public class BeanPostProcessorImpl implements BeanPostProcessor {
   @Override
   public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
      System.out.println("Inside postProcessAfterInitialization method..!!");
      return new Employee();
   }

   @Override
   public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
      System.out.println("Inside postProcessBeforeInitialization method..!!");
      return new Employee();
   }
}
<?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-3.0.xsd">
    <bean id="emp"
        class="com.ashok.spring.core.bean.beanpostprocessor.beans.Employee"
        init-method="init" destroy-method="destroy">
    </bean>
    <bean
        class="com.ashok.spring.core.bean.beanpostprocessor.impl.BeanPostProcessorImpl" />
</beans>
package com.ashok.spring.core.bean.beanpostprocessor.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.core.bean.beanpostprocessor.beans.Employee;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/bean/beanpostprocessor/config/applicationContext.xml";
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      Employee emp = (Employee) context.getBean("emp");
      System.out.println(emp);
      context.close();
   }
}
Output
Inside postProcessBeforeInitialization method...!!
Employee Bean Object Initialization through init() method
Inside postProcessAfterInitialization method...!!
com.ashok.spring.core.bean.beanpostprocessor.beans.Employee@185d8b6
Employee Object Destroying through destroy() method.

Next Tutorial : Inversion of Control

Previous Tutorial : Nested Beans 
 

No comments:

Post a Comment