Back to: Spring Framework Tutorials
Spring Framework Bean Post Processors
In this article, I am going to discuss Spring Framework Bean Post Processors. Please read our previous article, where we discussed Spring Framework Bean Life Cycle. Now, we will be experimenting with the Post Processors of a bean.
What are Bean Post Processors in Spring Framework?
Bean Post Processors are an essential part of the Spring framework’s container. They provide a mechanism to customize and enhance the initialization and destruction process of beans managed by the Spring container. Bean Post Processors intercept the lifecycle of beans, allowing developers to apply custom logic before and after bean initialization.
In Spring, the bean lifecycle consists of several stages, such as instantiation, property population, and initialization. Bean Post Processors come into play during the initialization phase, allowing developers to modify the bean instances just before they are fully initialized and ready for use.
Bean Post Processors can be used for various purposes in the Spring framework. Some common use cases include:
- Dependency injection: Bean Post Processors can be used to inject dependencies into beans after their initialization, providing a way to perform custom wiring or injection logic.
- AOP (Aspect-Oriented Programming): Bean Post Processors can be used to apply aspects or interceptors to beans, allowing developers to add cross-cutting concerns such as logging, security, or transaction management.
- Bean validation: Post processors can be utilized to perform validation on bean instances after initialization, ensuring they meet specific criteria or constraints.
- Bean customization: Developers can use post processors to modify bean instances based on certain conditions or business rules, dynamically adjusting their behavior or properties.
Implementing Bean Post Processors
In this program, we will be building upon the previous “Hello World” project from the previous article. Currently, the contents of the project are as follows:
App.java
Beans.xml
HelloWorld.java
Also, ensure that the required JAR files are in the project’s “Referenced Libraries” folder.
Step 1: Create a new file called HelloWorldInit.java in the src/ directory. Add the following lines of code to the file:
This class implements the BeanPostProcessor interface, which contains two methods: postProcessBeforeInitialization() and postProcessAfterInitialization(). These methods are called for each bean being initialized, and developers can provide custom logic within these methods to modify the bean instance or perform additional initialization steps.
The postProcessBeforeInitialization() method is invoked right before the init method of the bean is called. Developers can use this method to perform any pre-initialization customization on the bean, such as modifying properties or performing additional setups.
Similarly, the postProcessAfterInitialization() method is called immediately after the init method of the bean has been invoked. This method allows developers to perform any post-initialization tasks or apply additional modifications to the bean.
Step 2: Modify Beans.xml as follows:
This line instructs the Spring Framework to create a bean of type HelloWorldInit.
Step 3: Compile and execute the application. Ensure compilation is successful. Ensure that the output is as expected:
Congratulations! You have completed the bean post processors application in Spring Framework!
The Complete Example Code
App.java
import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld hw = (HelloWorld) context.getBean("helloWorld"); hw.getMsg(); ((AbstractApplicationContext) context).registerShutdownHook(); } }
Beans.xml
<?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 = "helloWorld" class = "HelloWorld" init-method = "init" destroy-method = "destroy"> <property name = "msg" value = "Hello World!"/> </bean> <bean class = "HelloWorldInit" /> </beans>
HelloWorld.java
public class HelloWorld { private String msg; public void getMsg() {System.out.println(msg);} public void setMsg(String msg) {this.msg = msg;} public void init() {System.out.println("Bean is being initialised!");} public void destroy() {System.out.println("Bean is being destroyed!");} }
HelloWorldInit.java
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class HelloWorldInit implements BeanPostProcessor { public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException { System.out.println("Intialising..."); return bean; } public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException { System.out.println("Intialised!"); return bean; } }
In the next article, I am going to discuss the Spring Framework Bean Definition Inheritance with Examples. Here, in this article, I try to explain Spring Framework Bean Post Processors. I hope you enjoy this article.