In this post, We will learn about the Spring BeanPostProcessor Example example using a Demo Project.
A bean post-processor helps us to customize new bean instances created by the spring Container. If We want to implement some kind of custom logic just after the Spring container finishes instantiating, configuring, and initializing a bean then we usually plug in one or more BeanPostProcessor implementations.
In the case of multiple BeanPostProcessor instances, we can control the order of execution by setting the order
property or implement Ordered
interface
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cpm.kkhindigyan.org</groupId> <artifactId>SpringBeanPostProcessorsExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <org.springframework.version>5.2.13.RELEASE</org.springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> </dependencies> </project> |
Message.java
Java class which is registered as Spring bean in applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.kkhindigyan.model; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class Message { private Integer messageId; private String message; public Message() { System.out.println("Message Object is created"); } public Integer getMessageId() { return messageId; } public void setMessageId(Integer messageId) { System.out.println("setMessageId"); this.messageId = messageId; } public String getMessage() { return message; } public void setMessage(String message) { System.out.println("setMessage"); this.message = message; } @PostConstruct public void init() throws Exception { System.out.println("message bean is ready to use.."); } @PreDestroy public void destroy() throws Exception { System.out.println("message bean is going to destroy."); } } |
MyBeanPostProcessor.java
To create a bean post-processor in spring Framework:
- Create a class that implements the BeanPostProcessor interface.
- After that implements both callback methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.kkhindigyan.processor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import com.kkhindigyan.model.Message; public class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("Inside of postProcessAfterInitialization"); Message message = null; if(bean instanceof Message) { message = (Message)bean; message.setMessage("Setting bean value in postProcessAfterInitialization"); } return message; } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("Inside of postProcessBeforeInitialization"); return bean; } } |
applicationContext.xml
Spring Config file there we have to register MyBeanPostProcessor class as spring bean with other Spring beans
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="message" class="com.kkhindigyan.model.Message"> <property name="messageId" value="101"></property> <property name="message" value="Welcome to Spring Framework!!"></property> </bean> <bean class="com.kkhindigyan.processor.MyBeanPostProcessor"></bean> </beans> |
ClientTest.java
Client Program with the main method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.kkhindigyan.org; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.kkhindigyan.model.Message; public class ClientTest { public static void main(String[] args) { ApplicationContext applicationContext = null; try { //Creating Instance of ApplicationContext Spring Container applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //Asking Spring Container to return Spring bean with id "message" Object object = applicationContext.getBean("message"); //Covert Spring bean into your business Object Message message = (Message)object; //Print Spring bean state System.out.println(message.getMessageId()+"\t"+message.getMessage()); } catch (Exception e) { e.printStackTrace(); }finally { if (applicationContext != null) ((AbstractApplicationContext) applicationContext).close(); } } } |
If you run ClientTest.java as Java Application then it will give the below output:
1 2 3 4 5 6 7 8 9 |
Message Object is created setMessageId setMessage Inside of postProcessBeforeInitialization message bean is ready to use.. Inside of postProcessAfterInitialization setMessage 101 Setting bean value in postProcessAfterInitialization message bean is going to destroy. |
That’s all about Spring BeanPostProcessor Example
You May Also Like:
Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?
If you have any feedback or suggestion please feel free to drop in below comment box.