Spring BeanPostProcessor Example

By Arvind Rai, October 29, 2021
On this page we will learn BeanPostProcessor interface. The BeanPostProcessor interface works as factory hook that allows for custom modification of new bean instances. The BeanPostProcessor interface has following methods.
1.
Object postProcessBeforeInitialization(Object bean, String beanName) 
This method is called before any bean initialization callbacks such as afterPropertiesSet method of InitializingBean or a custom init-method. The properties of bean will already be populated before calling postProcessBeforeInitialization method. The returned bean by this method can be a wrapper around the original.
2.
Object postProcessAfterInitialization(Object bean, String beanName) 
This method is called after any bean initialization callbacks such as afterPropertiesSet method of InitializingBean or a custom init-method.

The BeanPostProcessor can be used for following purposes.
1. Checking for marker interfaces using postProcessBeforeInitialization() method.
2. Wrapping beans with proxies using postProcessAfterInitialization method.

BeanPostProcessor with XML Configuration

Find the code to use BeanPostProcessor with XML configuration.
MyBeanPostProcessor.java
package com.concretepage;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println("Inside post process after initialization: "+beanName);
		return bean;
	}
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println("Inside post process before initialization: "+beanName);
		return bean;
	}
} 
app-conf.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.xsd">

    <bean id="animal" class="com.concretepage.Animal" init-method="init" destroy-method="destroy">
    	<property name="name" value="Lion"/>
    </bean>
    <bean class="com.concretepage.MyBeanPostProcessor"/>
</beans> 
Animal.java
package com.concretepage;
public class Animal {
	private String name;
	public void init() {
		System.out.println("Inside init method");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void destroy() {
		System.out.println("Inside destroy method");
	}
} 
SpringDemo.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo {
	public static void main(String[] args) {
		AbstractApplicationContext  context = new ClassPathXmlApplicationContext("app-conf.xml");
		Animal animal=(Animal)context.getBean("animal");
		System.out.println(animal.getName());
    	        context.registerShutdownHook();
	}
}  
Find the output.
Inside post process before initialization: animal
Inside init method
Inside post process after initialization: animal 
Lion
Inside destroy method

BeanPostProcessor with JavaConfig

Find the code to use BeanPostProcessor with JavaConfig.
MyBeanPostProcessor.java
package com.concretepage;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println("Inside post process before initialization: "+beanName);
		return bean;
	}
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println("Inside post process after initialization: "+beanName);
		return bean;
	}
} 
ElephantService.java
package com.concretepage;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Component;
@Component
public class ElephantService {
	@PostConstruct
	public void init() {
		System.out.println("Inside init method");
	}	
	public void print() {
		System.out.println("Hello World!");
	}
	@PreDestroy
	public void destroy() {
		System.out.println("Inside destroy method");		
	}
} 
AppConfig.java
package com.concretepage;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages="com.concretepage")
public class AppConfig {
} 
SpringDemo.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringDemo {
	public static void main(String[] args) {
	    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	    ctx.register(AppConfig.class);
	    ctx.refresh();
	    ElephantService service = ctx.getBean(ElephantService.class);
	    service.print();
    	    ctx.registerShutdownHook();
	}
}  
Find the output.
Inside post process before initialization: appConfig
Inside post process after initialization: appConfig
Inside post process before initialization: elephantService
Inside init method
Inside post process after initialization: elephantService
Hello World!
Inside destroy method 

Reference

Interface BeanPostProcessor

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us