Home  >  Spring Core

Example of BeanPostProcessor in Spring using XML and Annotation

By Arvind Rai, April 22, 2013
On this page we will provide example of BeanPostProcessor in spring using XML and annotation. BeanPostProcessor is called before and after bean initialization in container. BeanPostProcessor is used to check for marker interface or wrapping bean with proxies. BeanPostProcessor is called for each and every bean initialization in spring container. Here we will provide complete example with XML as well as annotation approach.

Spring BeanPostProcessor

Spring BeanPostProcessor allows to perform custom modification while initializing bean instance. This is an interface that we need to implement in our class and that class will be declared as bean and then spring ApplicationContext will auto detect the BeanPostProcessor. BeanPostProcessor has two methods as follows.

postProcessBeforeInitialization() : This method is called before bean initialization method. Bean initialization method is created using init-method bean attribute or @PostConstruct annotation. Its returns the bean which will be a wrapper around the original.

postProcessAfterInitialization() : This method will be called after bean initialization method.

BeanPostProcessor Example using XML

To use BeanPostProcessor with XML, we first create a class implementing it and then register it in spring application context XML file.
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 Example using Annotation

To use BeanPostProcessor with annotation, create it as bean and then ApplicationContext will automatically detect it and will be used for each and every bean registration with spring container.
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 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us