The largest Interview Solution Library on the web


« Previous | 2 | | 4 | Next »

Spring - Bean Post Processors

  • The BeanPostProcessor interface defines callback methods that you can implement to provide your own instantiation logic, dependency-resolution logic etc.
  •   
  • You can also implement some custom logic after the Spring container finishes instantiating, configuring, and initializing a bean by plugging in one or more BeanPostProcessor implementations.
  •   
  • You can configure multiple BeanPostProcessor interfaces and you can control the order in which these BeanPostProcessor interfaces execute by setting the order property provided the BeanPostProcessor implements the Ordered interface.
  •   
  • The BeanPostProcessors operate on bean (or object) instances which means that the Spring IoC container instantiates a bean instance and then BeanPostProcessor interfaces do their work.
  •   
  • An ApplicationContext automatically detects any beans that are defined with implementation of the BeanPostProcessor interface and registers these beans as post-processors, to be then called appropriately by the container upon bean creation.
  •     

    Spring - Bean Post Processors Example

    HelloWorld.java

    package com.jtc;
    public class HelloWorld {
    private String message;
    public void setMessage(String message){
    this.message = message;
    }
    public void getMessage(){
    System.out.println("Your Message : " + message);
    }
    public void init(){
    System.out.println("Inside Bean init.");
    }
    public void destroy(){
    System.out.println("Inside Bean destroy.");
    }
    }

    InitHelloWorld.java

    package com.jtc;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.beans.BeansException;
    public class InitHelloWorld implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("BeforeInitialization : " + beanName);
    return bean; // you can return any other object as well
    }
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("AfterInitialization : " + beanName);
    return bean; // you can return any other object as well
    }
    }

    MainApp.java

    package com.jtc;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class MainApp {
    public static void main(String[] args) {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
    obj.getMessage();
    context.registerShutdownHook();
    }
    }

    Beans.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-3.0.xsd">
    
       <bean id="helloWorld" class="com.jtc.HelloWorld"
           init-method="init" destroy-method="destroy">
           <property name="message" value="Hello World!"/>
       </bean>
    
       <bean class="com.jtc.InitHelloWorld" />
    
    </beans>

    Add Below Libraries
    commons-logging-1.1.1
    spring-aop-4.1.6.RELEASE
    spring-aspects-4.1.6.RELEASE
    spring-beans-4.1.6.RELEASE
    spring-context-4.1.6.RELEASE
    spring-context-support-4.1.6.RELEASE
    spring-core-4.1.6.RELEASE
    spring-expression-4.1.6.RELEASE
    spring-instrument-4.1.6.RELEASE
    spring-instrument-tomcat-4.1.6.RELEASE
    spring-jdbc-4.1.6.RELEASE
    spring-jms-4.1.6.RELEASE
    spring-messaging-4.1.6.RELEASE
    spring-orm-4.1.6.RELEASE
    spring-oxm-4.1.6.RELEASE
    spring-test-4.1.6.RELEASE
    spring-tx-4.1.6.RELEASE
    spring-web-4.1.6.RELEASE
    spring-webmvc-4.1.6.RELEASE
    spring-webmvc-portlet-4.1.6.RELEASE
    spring-websocket-4.1.6.RELEASE
    OutPut
    BeforeInitialization : helloWorld
    Inside Bean init.
    AfterInitialization : helloWorld
    Your Message : Hello World!
    Inside Bean destroy.

    Note:

    Here you need to register a shutdown hook registerShutdownHook() method that is declared on the AbstractApplicationContext class.
    This will ensures a graceful shutdown and calls the relevant destroy methods.

    « Previous | 1 | 2 | 3 | Next »


    copyright © 2014 - all rights riserved by javatechnologycenter.com