People like the wind into the jiangyun, like rain after sticky catkin.

An overview of the

The bean post-processor allows you to customize new bean instances created by spring Bean Factory. If you want to implement some custom logic after the Spring container has instantiated, configured, and initialized the bean, we can insert one or more BeanPostProcessor implementations.

If you have multiple instances of BeanPostProcessor, you can control the order of execution by setting the Order attribute or implementing the Ordered interface.

Spring BeanPostProcessor

BeanPostProcessor interface is composed of two callback methods, namely postprocessbeforeinitialize () and postprocessafterinitialize ().

For each bean instance created by the container, the post-handler gets the callback from the container before invoking the container initialization method and after any bean initializes the callback.

The post-bean processor usually checks the callback interface or wraps the bean with a proxy. Such as some Spring AOP infrastructure (for example AbstractAdvisingBeanPostProcessor) achieved after bean processors, providing packaging agent logic.

How to createBeanPostProcessor

After creating a bean handler in Spring:

  1. implementationBeanPostProcessorInterface.
  2. Implement the callback method.
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
 
public class CustomBeanPostProcessor implements BeanPostProcessor
{
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
    {
        System.out.println("Called postProcessBeforeInitialization() for :" + beanName);
        return bean;
    }
     
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
    {
        System.out.println("Called postProcessAfterInitialization() for :" + beanName);
        returnbean; }}Copy the code

How to sign upBeanPostProcessor

ApplicationContext automatically detects all beans defined in the configuration metadata that implements the BeanPostProcessor interface. It registers these beans as post-handlers so that they can be invoked later when the beans are created

Spring will then pass each bean instance to these two methods before and after invoking the initialization callback method, where you can process the bean instance however you like.


<beans>
     <bean id="customBeanPostProcessor"
               class="com.howtodoinjava.demo.processors.CustomBeanPostProcessor" />
</beans>
Copy the code

BeanPostProcessorWhen a method is called

Normally, Spring’s DI container does the following to create a bean:

  1. Recreate it through a constructor or factory methodbeanThe instance
  2. Sets property values and values for othersbeanA reference to the
  3. Call all*AwareDefined in the interfacesettermethods
  4. willbeanInstances are passed to eachbeanpost-processorPostProcessBeforeInitialization ()methods
  5. Invoke the initialization callback method
  6. willBeanInstances are passed to eachBeanpost-processorPostProcessAfterInitialization ()methods
  7. thisbeanIt’s ready to be used
  8. When the container is closed, the destruction callback method is called

Spring BeanPostProcessorThe sample

To show example usage, I use the EmployeeDAOImpl class, as follows:

public class EmployeeDAOImpl implements EmployeeDAO
{
    public EmployeeDTO createNewEmployee(a)
    {
        EmployeeDTO e = new EmployeeDTO();
        e.setId(1);
        e.setFirstName("Lokesh");
        e.setLastName("Gupta");
        return e;
    }
     
    public void initBean(a) {
        System.out.println("Init Bean for : EmployeeDAOImpl");
    }
     
    public void destroyBean(a) {
        System.out.println("Init Bean for : EmployeeDAOImpl"); }}Copy the code

The bean and its post-handler are configured as follows:

<bean id="customBeanPostProcessor" class="com.howtodoinjava.demo.processors.CustomBeanPostProcessor" />
     
<bean id="dao" class="com.howtodoinjava.demo.dao.EmployeeDAOImpl"  init-method="initBean" destroy-method="destroyBean"/>
Copy the code

Now, start the DI container and view the output:

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Copy the code

Output:

Called postProcessBeforeInitialization(a) for : dao
Init Bean for : EmployeeDAOImpl
Called postProcessAfterInitialization(a) for : dao
Copy the code

Obviously, the BeanPostProcessor method is called before and after the initialization method.

Spring BeanPostProcessor Example