Spring - Bean Post Processors

BeanPostProcessor接口定义了回调方法,可以实现这些方法来提供自己的实例化逻辑、依赖项解析逻辑等。还可以在Spring容器完成实例化、配置和初始化bean后,通过插入一个或多个BeanPostProcessor实现来实现一些自定义逻辑。

可以配置多个BeanPostProcessor接口,并且可以通过设置order属性来控制这些BeanPostProcessor接口的执行顺序,前提是BeanPostProcessor实现了Ordered接口。

BeanPostProcessor操作bean(或对象)实例,这意味着SpringIOC容器实例化一个bean实例,然后BeanPostProcessor接口完成它们的工作。

ApplicationContext自动检测通过BeanPostProcessor接口的实现定义的任何bean,并将这些bean注册为后处理器,然后在创建bean时由容器适当地调用。

例子

下面的示例演示如何在ApplicationContext的上下文中编写,注册和使用BeanPostProcessors。

让我们拥有一个可以正常运行的Eclipse IDE,并执行以下步骤来创建一个Spring应用程序:

HelloWorld.java文件的内容-

package com.tutorialspoint;

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("Bean is going through init.");
   }
   public void destroy(){
      System.out.println("Bean will destroy now.");
   }
}

这是实现BeanPostProcessor的一个非常基本的示例,该示例在初始化任何bean之前和之后都显示bean名称。可以在初始化bean之前和之后实现更复杂的逻辑,因为可以在两个后处理器方法中访问bean对象。

InitHelloWorld.java文件的内容-

package com.tutorialspoint;

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文件的内容。在这里,需要注册一个在AbstractApplicationContext类上声明的关闭钩子registerShutdownHook()方法。这将确保正常关机并调用相关的destroy方法。

package com.tutorialspoint;

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

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

</beans>

完成创建源和Bean配置文件后,运行该应用程序。它将显示以下消息-

BeforeInitialization : helloWorld
Bean is going through init.
AfterInitialization : helloWorld
Your Message : Hello World!
Bean will destroy now.

本文使用 文章同步助手 同步

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容