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
- 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
- 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
- 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
- <?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
- <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
- 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.
hi Kb,
I start reading your blog & find unique topic explanation .thanks for great work !!!
Thanks !!