Bean Post Processor
In this tutorial, we are going to discuss bean post processor in the spring framework. In Spring Framework, when we activate the ApplicationContext container, the ApplicationContext container will automatically perform the following actions without the developer’s interaction.
- ApplicationContext will read beans definitions from the beans configuration file.
- ApplicationContext will recognize beans classes and create objects for bean classes.
- ApplicationContext will perform initialization by executing initialization methods.
- When IOCContainer is shut down then bean objects are destroyed.
In the above Bean lifecycle, if we want to provide customization’s over beans initializations, then we have to use a predefined interface provided by Spring framework like
“org.springframework.beans.factory.config.BeanPostProcessor”.
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 the 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 the init() method if we provide custom initialization method init().
To use BeanPostPrcessor in Spring applications, we must declare a user-defined class as an implementation class to the BeanPostProcessor interface. We must implement the above-specified methods and configure the implementation class in the bean configuration file. If we provide like this, the container will automatically detect BeanPostProcessor implementation, and IOC Container will execute the BeanPostProcessor methods in the respective locations of the bean’s life cycle.
E.g
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");
}
}
E.g
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;
/**
*
* @author Ashok Kumar
*
*/
public class BeanPostProcessorImpl implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
System.out.println("Inside postProcessAfterInitialization method..!!");
return new Employee();
}
@Override
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="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="emp"
class="com.ashok.spring.core.bean.beanpostprocessor.beans.Employee"
init-method="init" destroy-method="destroy">
</bean>
<bean
class="com.ashok.spring.core.bean.beanpostprocessor.impl.BeanPostProcessorImpl" />
</beans>
package com.ashok.spring.core.bean.beanpostprocessor.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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");
System.out.println(emp);
context.close();
}
}
E.g
Inside postProcessBeforeInitialization method...!!
Employee Bean Object Initialization through init() method
Inside postProcessAfterInitialization method...!!
com.ashok.spring.core.bean.beanpostprocessor.beans.Employee@185d8b6
Employee Object Destroying through destroy() method.