Bean Post Processors in Spring

Bean Post Processors in Spring

Spring beans are very flexible and versatile technology. Thanks to that, you can develop your Spring application as you want. One of the best solutions in Spring is Post Processors.

– you can provide your application with custom logic by implementing BeanPostProcessor interface. You can do a custom modification

– if you have more BeanPostProcessor interfaces, you can control the order they are implemented by implementing Order interface

Spring IoC Container instantiates a bean instance and then BeanPostProcessor interfaces do their work

ApplicationContext  detects any beans automatically. They are defined with implementation of theBeanPostProcessor interface. ApplicationContext registers these beans as post-processors, and they are called appropriately by the container during bean creation

package com.linaittech;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;

public class Mirello implements BeanPostProcessor {

public Object postProcessBeforeInitialization(Object beanObject, String beanName) throws BeansException {

System.out.println(“Before the bean initialization : ” + beanName);

return beanObject;

}

public Object postProcessAfterInitialization(Object beanObject, String beanName) throws BeansException {

System.out.println(“After the bean initialization : ” + beanName);

return beanObject;

}

}

The class Mirello implements BeanPostProcessor interface to use. We import the interface from org.springframework.beans.factory.config.BeanPostProcessor to use the two methods: postProcessBeforeInitialization i postProcessAfterInitialization.

postProcessBeforeInitialization is applied to a new bean instance before any bean initialization callbacks. In other words, it’s used to be sure required actions are taken before initialization.

postProcessAfterInitialization is applied to a new bean instance after any bean initialization callbacks. In other words, it’s used to be sure required actions are taken after initialization but before destruction.

Both these methods take two arguments:

beanObject – the instance of our bean

beanName – the name of our bean

The life cycle of a Spring bean with Post Processor is following :

1/ BeanPostProcessor.postProcessBeforeInitialization()

2/ init()

3/ BeanPostProcessor.postProcessAfterInitialization()

4/ destroy()

So thanks to BeanPostProcessor, we can do something before our bean initialization and before our bean destruction. Sometimes, it’s very useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s