Spring BeanPostProcessor

In this post of Spring core, we will learn as how Spring bean post processor works. The bean post processor provides the flexibility to do some additional bean processing before and after bean initialization. The Spring bean post processor is responsible to process every bean instance available with the IoC container.

 

Introduction

Before we get in to the details of Spring bean post processor, let’s look at the different steps of the bean initialization and where bean post processor fit in to the picture.

Spring BeanPostProcessor

Let’s see what all steps takes place during Spring Bean life cycle.

  1. Spring loads bean definitions by scanning the classes with the configuration, component annotations. It also load the bean definition by parsing any bean XML files.
  2. Bean definitions added to BeanFactory.
  3. During the bean creation process, Spring DI will come in to picture to address any dependencies.
  4. BeanPostProcessor will allow us to do some additional bean processing before and after bean initialization.
  5. The Spring bean is ready to use.

[pullquote align=”normal”]Bean post processor is helpful to validate the bean definition or can alter the bean properties based on certain requirements.[/pullquote]

 

1. Spring BeanPostProcessor

The BeanPostProcessor is a factory hook that allows for custom modification for the new bean. This interface defines 2 methods the implementing class should implement.

  • postProcessAfterInitialization – Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks.
  • postProcessBeforeInitialization – Apply this BeanPostProcessor to the given new bean instance before any bean initialization.

Let’s see a simple example for a better understanding:

public class CustomBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("Calling bean post processor before init for bean:: "+beanName + "class name: "+bean.getClass());
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
                System.out.println("Calling bean post processor after init for bean:: "+beanName + "class name: "+bean.getClass());
        return bean;
    }
}

Keep in mind that Spring IoC will pass each bean to this custom BeanPostProcessor.Also please note that this differs from the @PostConstruct annotation.The @PostConstruct is just used for the specific class we want to customize.

[pullquote align=”normal”]@PostConstruct and @PreDestroy annotations are generally considered best practice for receiving life-cycle callbacks in a modern Spring application. [/pullquote]

 

2 How to Register BeanPostProcessor

The easiest way to register the Spring BeanPostProcessor is by annotating the class with @Component or define the bean in bean.xml file or in the configuration file:

@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("Calling bean post processor before init for bean:: "+beanName + "class name: "+bean.getClass());
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("Calling bean post processor after init for bean:: "+beanName + "class name: "+bean.getClass());
        return bean;
    }
}

We can also define this in the Configuration class like:

public class CustomBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("Calling bean post processor before init for bean:: "+beanName + "class name: "+bean.getClass());
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("Calling bean post processor after init for bean:: "+beanName + "class name: "+bean.getClass());
        return bean;
    }
}

@Configuration
public class MyConfiguration {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }

    @Bean
    public CustomBeanPostProcessor customBeanPostProcessor() {
        return new CustomBeanPostProcessor();
    }
}

 

Summary

In this class we inspect the BeanPostProcessor. We saw how Spring bean post processor works and how we can use this feature for the custom modification of new bean instances.