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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
    }
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
    }
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
    }
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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>
<?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
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
<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>
<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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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();
        
    }
}
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

Karibasappa G C (KB)
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