 
- Spring 核心基础知识
- 春天 - 主页
- 春天 - 概述
- 春天-建筑
- Spring - 环境设置
- Spring - Hello World 示例
- Spring - IoC 容器
- Spring - Bean 定义
- Spring - Bean 范围
- Spring - Bean 生命周期
- Spring - Bean 后处理器
- Spring-Bean定义继承
- Spring - 依赖注入
- Spring - 注入内部 Bean
- Spring - 注入集合
- Spring - Bean 自动装配
- 基于注释的配置
- Spring - 基于Java的配置
- Spring - Spring 中的事件处理
- Spring - Spring 中的自定义事件
- Spring - 使用 Spring 框架的 AOP
- Spring - JDBC 框架
- Spring-事务管理
- Spring - Web MVC 框架
- Spring - 使用 Log4J 进行日志记录
- 春季问答
- 春天 - 问题与解答
- 春季有用资源
- 春天 - 快速指南
- Spring - 有用的资源
- 春天 - 讨论
Spring - Bean 后处理器
BeanPostProcessor接口定义了回调方法,您可以实现这些方法来提供自己的实例化逻辑、依赖解析逻辑等。您还可以在 Spring 容器完成实例化、配置和初始化 Bean 后,通过插入一个或多个来实现一些自定义逻辑。 BeanPostProcessor 实现。
您可以配置多个 BeanPostProcessor 接口,并且可以通过设置order属性来控制这些 BeanPostProcessor 接口的执行顺序(前提是 BeanPostProcessor 实现了Ordered接口)。
BeanPostProcessor 对 bean(或对象)实例进行操作,这意味着 Spring IoC 容器实例化一个 bean 实例,然后 BeanPostProcessor 接口完成它们的工作。
ApplicationContext自动检测通过BeanPostProcessor接口的实现定义的任何 bean,并将这些 bean 注册为后处理器,然后在创建 bean 时由容器适当地调用。
例子
以下示例展示了如何在 ApplicationContext 的上下文中编写、注册和使用 BeanPostProcessor。
让我们准备好一个可用的 Eclipse IDE,并按照以下步骤创建一个 Spring 应用程序 -
| 脚步 | 描述 | 
|---|---|
| 1 | 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包com.tutorialspoint 。 | 
| 2 | 使用“添加外部 JAR”选项添加所需的 Spring 库,如Spring Hello World 示例一章中所述。 | 
| 3 | 在com.tutorialspoint包下创建 Java 类HelloWorld、InitHelloWorld和MainApp。 | 
| 4 | 在src文件夹下创建Beans配置文件Beans.xml。 | 
| 5 | 最后一步是创建所有 Java 文件和 Bean 配置文件的内容并运行应用程序,如下所述。 | 
这是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文件的内容。这里您需要注册一个关闭钩子registerShutdownHook()方法,该方法在 AbstractApplicationContext 类上声明。这将确保正常关闭并调用相关的销毁方法。
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();
   }
}
 
以下是init 和 destroy 方法所需的配置文件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.