English 中文(简体)
Spring - Bean Post Processors
  • 时间:2024-12-22

Spring - Bean Post Processors


Previous Page Next Page  

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 initiapzing 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 ApppcationContext automatically detects any beans that are defined with the implementation of the BeanPostProcessor interface and registers these beans as postprocessors, to be then called appropriately by the container upon bean creation.

Example

The following examples show how to write, register, and use BeanPostProcessors in the context of an ApppcationContext.

Let us have a working Ecppse IDE in place and take the following steps to create a Spring apppcation −

Steps Description
1 Create a project with a name SpringExample and create a package com.tutorialspoint under the src folder in the created project.
2 Add required Spring pbraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld, InitHelloWorld and MainApp under the com.tutorialspoint package.
4 Create Beans configuration file Beans.xml under the src folder.
5 The final step is to create the content of all the Java files and Bean Configuration file and run the apppcation as explained below.

Here is the content of HelloWorld.java file −

package com.tutorialspoint;

pubpc class HelloWorld {
   private String message;

   pubpc void setMessage(String message){
      this.message  = message;
   }
   pubpc void getMessage(){
      System.out.println("Your Message : " + message);
   }
   pubpc void init(){
      System.out.println("Bean is going through init.");
   }
   pubpc void destroy(){
      System.out.println("Bean will destroy now.");
   }
}

This is a very basic example of implementing BeanPostProcessor, which prints a bean name before and after initiapzation of any bean. You can implement more complex logic before and after intiapzing a bean because you have access on bean object inside both the post processor methods.

Here is the content of InitHelloWorld.java file −

package com.tutorialspoint;

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

pubpc class InitHelloWorld implements BeanPostProcessor {
   pubpc Object postProcessBeforeInitiapzation(Object bean, String beanName) 
      throws BeansException {
      
      System.out.println("BeforeInitiapzation : " + beanName);
      return bean;  // you can return any other object as well
   }
   pubpc Object postProcessAfterInitiapzation(Object bean, String beanName) 
      throws BeansException {
      
      System.out.println("AfterInitiapzation : " + beanName);
      return bean;  // you can return any other object as well
   }
}

Following is the content of the MainApp.java file. Here you need to register a shutdown hook registerShutdownHook() method that is declared on the AbstractApppcationContext class. This will ensures a graceful shutdown and calls the relevant destroy methods.

package com.tutorialspoint;

import org.springframework.context.support.AbstractApppcationContext;
import org.springframework.context.support.ClassPathXmlApppcationContext;

pubpc class MainApp {
   pubpc static void main(String[] args) {
      AbstractApppcationContext context = new ClassPathXmlApppcationContext("Beans.xml");

      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
      context.registerShutdownHook();
   }
}

Following is the configuration file Beans.xml required for init and destroy methods −

<?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.tutorialspoint.HelloWorld"
      init-method = "init" destroy-method = "destroy">
      <property name = "message" value = "Hello World!"/>
   </bean>

   <bean class = "com.tutorialspoint.InitHelloWorld" />

</beans>

Once you are done with creating the source and bean configuration files, let us run the apppcation. If everything is fine with your apppcation, it will print the following message −

BeforeInitiapzation : helloWorld
Bean is going through init.
AfterInitiapzation : helloWorld
Your Message : Hello World!
Bean will destroy now.
Advertisements