Reading:  

Quick to the point introduction to Spring Framework


Bean Post Processors

The interface BeanPostProcessor defines methods of callback where user can implement their own logic of dependency-resolution, instantiation etc. User can configure multiple BeanPostProcessor interfaces and can be execute by setting the order property.

To operate on object instances by BeanPostProcessors that the Spring container IoC instantiates a instance and then interfaces BeanPostProcessor do their task.

The bean which is defined by implementation of the interface BeanPostProcessor is automatically finds by ApplicationContext and these beans are registers as post-processors and called appropriately by the container upon bean creation.

Example:

Let's start with our Customer class

Java class: Customer

package pack1;  
  
public class Customer {  
private String cust_name;  
  
public String getCname() {  
    System.out.println("Customer Name: "+ cust_name);  
  }  
public void setCname(String cust_name) {  
    this.cust_name = cust_name;  
   }  
public void ExmInit(){
      System.out.println("In init block of Customer");
   }
   public void destroy(){
      System.out.println("In destroy block of Customer");
   }
}  

Java class: CustInit which implements BeanPostProcessor

package pack1;  

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

public class CustInit implements BeanPostProcessor {
 
   public Object postProcessBeforeInitialization(Object bean,
                 String Name) throws BeansException {
      System.out.println("postProcessBeforeInitialization: " + Name);
      return bean;  
   }

   public Object postProcessAfterInitialization(Object bean,
                 String Name) throws BeansException {
      System.out.println("postProcessAfterInitialization: " + Name);
      return bean;  
   }
}
 

Java file: ExmTest

User need to register registerShutdownHook() method is a shutdown hook, is declared on the class AbstractApplicationContext which will ensures a shutdown and calls the relevant destroy methods.

package pack1;  
  
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
  
public class ExmTest {  
public static void main(String[] args) {  
AbstractApplicationContext
 cont = new ClassPathXmlApplicationContext("SingleBeans.xml");

    Customer firstCust=(Customer)cont.getBean("Customer");  
    firstCust.getCname();
  firstCust.registerShutdownHook();
             }  
}  
 

The configuration file is SingleBeans.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="Customer" class="pack1.Customer" 
      init-method="ExmInit" destroy-method="destroy">
       <property name="cust_name" value="Nimrit"/>
   </bean>
    <bean class="pack1.CustInit " />
  </beans>  

Compile and run the program will get the below result:

Result:

postProcessBeforeInitialization: Nimrit 
In init block of Customer
postProcessAfterInitialization: Nimrit 
Customer Name: Nimrit 
In destroy block of Customer

 

Description

This tutorial covers various topics releated to Spring Framework as listed below

  • Framework Overview
  • Modules
  • Environment Setup
  • Saying Hello World with Spring
  • IoC Containers
  • Bean Definition
  • Bean Scopes 
  • Bean Life Cycle
  • Bean Post Processors
  • Bean Definition Inheritance
  • Dependency Injection
  • Injecting Inner Beans
  • Injecting Collection
  • Beans Auto-Wiring
  • Annotation Based Configuration
  • Java Based Configuration
  • Event Handling in Spring
  • Custom Events in Spring
  • JDBC Framework Overview
  • Transaction Management

 

 



Prerequisites

Prior knowledge of Java is essential

Audience

Beginners or students seeking quick introduction to Spring

Learning Objectives

This tutorial is for beginners seeking quick introduction to Spring Framework.

Author: Subject Coach
Added on: 22nd Jun 2015

You must be logged in as Student to ask a Question.

None just yet!