Spring Bean Post Processors

BeanPostProcessor provides a way to perform some operations before creating a spring bean and immediately after creating spring bean. So you can add some logic before and after creating the bean.

Follow steps to implement spring bean post processors –

  • Implements BeanPostProcessor in Processor class.
  • Overrides methods provided by BeanPostProcessor and add some logic whatever you want in postProcessBeforeInitialization and postProcessAfterInitialization
  • Create bean for the class who implement BeanPostProcessor in .xml file

Lets Implement

Project structure be like below image

3

add a dependency on pom.xml file –

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>4.3.4.RELEASE</version>
</dependency>

bean class “Greetings.java

package com.spring.core;
public class Greetings {
    private String message;
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public void init() {
        System.out.println("Init");
    }
    public void destroy() {
        System.out.println("Destroy");
    }
}

Processor.java

package com.spring.core;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class Processor implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("After Process Initialization Bean - "+bean+" and Name - "+beanName);
        return bean;
    }
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Before Process Initialization Bean - "+bean+" and Name - "+beanName);
        return bean;
    }
}

beans.xml file is in classpath

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
        "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans default-init-method="init" default-destroy-method="destroy">
    <bean id="greet" class="com.spring.core.Greetings">
        <property name="message" value="Hello One"></property>
    </bean>
    <bean id="processor" class="com.spring.core.Processor"></bean>
</beans>

Main class

package com.spring.core;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application { 
    public static void main(String[] args) {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Greetings greet = (Greetings) context.getBean("greet"); 
        System.out.println(greet.getMessage());
        context.registerShutdownHook();
    }
}

Output –

Dec 28, 2016 12:31:32 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Wed Dec 28 12:31:32 IST 2016]; root of context hierarchy
Dec 28, 2016 12:31:32 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Before Process Initialization Bean - com.spring.core.Greetings@14bf9759 and Name - greet
Init
After Process Initialization Bean - com.spring.core.Greetings@14bf9759 and Name - greet
Hello One
Dec 28, 2016 12:31:32 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Wed Dec 28 12:31:32 IST 2016]; root of context hierarchy
Destroy

For more details – watch video

Thanks for reading 🙂

Please Subscribe our you tube channel Almighty Java

Leave a comment