Spring bean life cycle methods – BeanPostProcessor

BeanPostProcessor is used to perform some operations before and after creating a bean,this allows us to add some code before and after creating the bean.

BeanPostProcessor is applicable for all the beans, which means its methods will be executed for each bean we define in the xml.

We can use the BeanPostProcessor to execute some logic for all the beans in the application context before and after their initialization

BeanPostProcessor interface has 2 methods postProcessBeforeInitialization() and postProcessAfterInitialization() where former is called after the bean is created and before it is initialized And the latter is called after the bean initialization

Create a class which implements BeanPostProcessor

  1. package com.kb.beans;
  2.  
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.beans.factory.config.BeanPostProcessor;
  5.  
  6. public class MyBeanPostProcessor implements BeanPostProcessor {
  7.    
  8.     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  9.         System.out.println("Post Process After Initialization for the bean "+beanName);
  10.         return bean;
  11.     }
  12.  
  13.     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  14.         System.out.println("Post Process Before Initialization for the bean "+beanName);
  15.         return bean;
  16.     }
  17. }
package com.kb.beans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {
	
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("Post Process After Initialization for the bean "+beanName);
		return bean;
	}

	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("Post Process Before Initialization for the bean "+beanName);
		return bean;
	}
}

Now create 2 classes and make them as spring beans so that we can see how the BeanPostProcessor is executed for both beans

Create a class called Person

  1. package com.kb.beans;
  2.  
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.beans.factory.config.BeanPostProcessor;
  5.  
  6. public class Person {
  7.  
  8.     public Person() {
  9.     System.out.println("Person - no argument constructor");
  10.     }
  11.     private String name;
  12.    
  13.     public void setName(String name) {
  14.         this.name = name;
  15.     }
  16.  
  17.     public String getName() {
  18.         return name;
  19.     }
  20. }
package com.kb.beans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class Person {

	public Person() {
	System.out.println("Person - no argument constructor");
	}
	private String name;
	
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
}

Create the User class

  1. package com.kb.beans;
  2.  
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.beans.factory.config.BeanPostProcessor;
  5.  
  6. public class User {
  7.  
  8.     public User() {
  9.         System.out.println("User - no argument constructor");
  10.     }
  11.  
  12.     private String name;
  13.  
  14.     public String getName() {
  15.         return name;
  16.     }
  17.  
  18.     public void setName(String name) {
  19.         this.name = name;
  20.     }
  21. }
package com.kb.beans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class User {

	public User() {
		System.out.println("User - no argument constructor");
	}

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

Create the spring configuration file

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xmlns:p="http://www.springframework.org/schema/p"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  8.        http://www.springframework.org/schema/context
  9.         http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  10.  
  11.     <bean id="person" class="com.kb.beans.Person">
  12.         <property name="name" value="Raj"/>
  13.     </bean>
  14.    
  15.     <bean id="user" class="com.kb.beans.User">
  16.     <property name="name" value="Ram"/>
  17.     </bean>
  18.    
  19.     <!-- This post processor methods are called for every bean -->
  20.     <bean class="com.kb.beans.MyBeanPostProcessor"/>
  21. </beans>
<?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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
  		http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<bean id="person" class="com.kb.beans.Person">
		<property name="name" value="Raj"/>
	</bean>
	
	<bean id="user" class="com.kb.beans.User">
	<property name="name" value="Ram"/>
	</bean>
	
	<!-- This post processor methods are called for every bean -->
	<bean class="com.kb.beans.MyBeanPostProcessor"/>
</beans>

Create pom.xml file as below

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.   <modelVersion>4.0.0</modelVersion>
  4.  
  5.   <groupId>Spring</groupId>
  6.   <artifactId>SpringCore</artifactId>
  7.   <version>0.0.1-SNAPSHOT</version>
  8.   <packaging>jar</packaging>
  9.  
  10.   <name>SpringCore</name>
  11.   <url>http://maven.apache.org</url>
  12.  
  13.   <properties>
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15.   </properties>
  16.  
  17.   <dependencies>
  18.   <dependency>
  19.     <groupId>org.springframework</groupId>
  20.     <artifactId>spring-core</artifactId>
  21.     <version>4.2.4.RELEASE</version>
  22. </dependency>
  23.   <dependency>
  24.     <groupId>org.springframework</groupId>
  25.     <artifactId>spring-context</artifactId>
  26.     <version>4.2.4.RELEASE</version>
  27. </dependency>
  28.  
  29.     <dependency>
  30.       <groupId>junit</groupId>
  31.       <artifactId>junit</artifactId>
  32.       <version>3.8.1</version>
  33.       <scope>test</scope>
  34.     </dependency>
  35.   </dependencies>
  36. </project>
<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>Spring</groupId>
  <artifactId>SpringCore</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringCore</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Create a BeansLifeCycle class as below

  1. package com.kb.beans;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6.  
  7. public class BeansLifeCycle {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
  12.        
  13.         //Load Person object
  14.         Person person = (Person)applicationContext.getBean("person");
  15.         System.out.println("Name : "+person.getName());
  16.        
  17.         //Load User object
  18.         User user = (User)applicationContext.getBean("user");
  19.         System.out.println("Name : "+user.getName());
  20.        
  21.         applicationContext.registerShutdownHook();
  22.        
  23.     }
  24. }
package com.kb.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class BeansLifeCycle {

	public static void main(String[] args) {

		ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
		
		//Load Person object
        Person person = (Person)applicationContext.getBean("person");
        System.out.println("Name : "+person.getName());
        
        //Load User object
        User user = (User)applicationContext.getBean("user");
        System.out.println("Name : "+user.getName());
        
        applicationContext.registerShutdownHook();
        
	}
}

Run this class and see the output as below

Look at the output, BeanPostProcessor’s methods are called for both the beans Person and User.

Download this project BeanPostProcessor.zip

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on