Recent Posts

Saturday, 8 June 2019

Bean Post Processor

     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
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.
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 {
   public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
      System.out.println("Inside postProcessAfterInitialization method..!!");
      return new Employee();

   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=""
    <bean id="emp"
        init-method="init" destroy-method="destroy">
        class="com.ashok.spring.core.bean.beanpostprocessor.impl.BeanPostProcessorImpl" />
package com.ashok.spring.core.bean.beanpostprocessor.test;


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");
Inside postProcessBeforeInitialization method...!!
Employee Bean Object Initialization through init() method
Inside postProcessAfterInitialization method...!!
Employee Object Destroying through destroy() method.

Next Tutorial : Inversion of Control

Previous Tutorial : Nested Beans 

No comments:

Post a Comment