March 10, 2017

#Spring part 4 : BeanFactoryPostProcessor and BeanPostProcessor..

BeanPostProcessor tells Spring to do some processing after initialization beans.

BeanPostProcessor is interface which defines callback methods, with these methods we can tell Spring to do some processing after initialization of beans. This allows you to add some custom logic before and after spring bean creation.

You need to configure, your own BeanPostProcessor in spring.xml (you can configure same BeanPostProcessor multiple times)
< bean class = "com.test.MyBeanPostProcessor" / >

When you implement BeanPostProcessor, you need to override postProcessBeforeInitialization() and postProcessAfterInitialization() methods:

public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName)
    throws BeansException {
return bean; // you can return any other object as well

public Object postProcessAfterInitialization(Object bean, String beanName)
    throws BeansException {
return bean; // you can return any other object as well

We can configure multiple BeanPostProcessor interfaces and we can control the order in which these BeanPostProcessor interfaces execute by setting the order property provided the BeanPostProcessor implements the Ordered interface.

When you implement Ordered, you need to override getOrder() method:
public int getOrder() {
    return 1;

The BeanPostProcessors operate on bean (or object) instances, which means that the Spring IoC container instantiates a bean instance and then BeanPostProcessor interfaces do their work.

An ApplicationContext automatically detects any beans that are defined with the implementation of the BeanPostProcessor interface and registers these beans as postprocessors, to be then called appropriately by the container upon bean creation.

public static void main(String[] args) {
AbstractApplicationContext  context=new ClassPathXmlApplicationContext("beanPostProcessorSpring.xml");
    Traingle traingle3=(Traingle)context.getBean("traingle");

We need to register a shutdown hook registerShutdownHook() method that is declared on the AbstractApplicationContext class. This will ensures a graceful shutdown and calls the relevant destroy methods.

BeanFactoryPostProcessor: When BeanPostProcessor is registered as a post-processor with the BeanFactory, for each bean instance that is created by the BeanFactory, the post-processor will get a callback from the BeanFactory before any initialization methods (afterPropertiesSet and any declared init method) are called, and also afterwords.

But with BeanFactoryPostProcessor, you can apply changes of some sort to an entire BeanFactory, after it has been constructed.

You need to Override postProcessBeanFactory() method when you implement BeanFactoryPostProcessor interface.

public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
    throws BeansException {
    System.out.println("postProcessBeanFactory is called..");       

BeanFactoryPostProcessor need to be configured in spring.xml in a similar way we have configured BeanPostProcessor,
< bean class = "com.test.MyBeanFactoryPostProcessor" / >

-K Himaanshu Shuklaa..

No comments:

Post a Comment

RSSChomp Blog Directory