Spring后置处理器(BeanPostProcessor)

本节主要介绍在《[Spring Bean生命周期]》一节提到的 BeanPostProcessor 接口。BeanPostProcessor 接口也被称为后置处理器,通过该接口可以自定义调用初始化前后执行的操作方法。

BeanPostProcessor 接口源码如下:


1.  public interface  BeanPostProcessor {
2.  Object  postProcessBeforeInitialization(Object  bean, String  beanName) throws BeansException;

4.  Object  postProcessAfterInitialization(Object  bean, String  beanName) throws BeansException;
5.  }

该接口中包含了两个方法:

  • postProcessBeforeInitialization() 方法:在 Bean 实例化、属性注入后,初始化前调用。
  • postProcessAfterInitialization() 方法:在 Bean 实例化、属性注入、初始化都完成后调用。

当需要添加多个后置处理器实现类时,默认情况下 Spring 容器会根据后置处理器的定义顺序来依次调用。也可以通过实现 Ordered 接口的 getOrder 方法指定后置处理器的执行顺序。该方法返回值为整数,默认值为 0,取值越大优先级越低。

示例

下面我们就来演示下 BeanPostProcessor 接口的用法,步骤如下:

1. 新建一个名为 SpringDemo 的 Java  项目,并在该项目引入 Spring 相关的依赖包。

2. 在 net.biancheng.c 包下,创建一个名为 HelloWorld 的类,代码如下。


1.  package net.biancheng.c;

3.  public class  HelloWorld {
4.  private String  message;

6.  public void setMessage(String  message) {
7.  this.message = message;
8.  }

10.  public void getMessage() {
11.  System.out.println("Message : " + message);
12.  }

14.  public void init() {
15.  System.out.println("Bean 正在初始化");
16.  }

18.  public void destroy() {
19.  System.out.println("Bean 将要被销毁");
20.  }
21.  }

3. 在 net.biancheng.c 包下,创建一个名为 InitHelloWorld 的类,代码如下。


1.  package net.biancheng.c;

3.  import org.springframework.beans.BeansException;
4.  import org.springframework.beans.factory.config.BeanPostProcessor;
5.  import org.springframework.core.Ordered;

7.  public class  InitHelloWorld implements BeanPostProcessor, Ordered {
8.  @Override
9.  public Object  postProcessBeforeInitialization(Object  bean, String  beanName) throws BeansException {
10.  System.out.println("A Before : " + beanName);
11.  return bean;
12.  }

14.  @Override
15.  public Object  postProcessAfterInitialization(Object  bean, String  beanName) throws BeansException {
16.  System.out.println("A After : " + beanName);
17.  return bean;
18.  }

20.  @Override
21.  public int getOrder() {
22.  return 5;
23.  }
24.  }

需要注意的是,postProcessBeforeInitialization 和 postProcessAfterInitialization 方法返回值不能为 null,否则会报空指针异常或者通过 getBean() 方法获取不到 Bean 实例对象。

4. 在 net.biancheng.c 包下,创建一个名为  InitHelloWorld2 的类,代码如下。


1.  package net.biancheng.c;

3.  import org.springframework.beans.BeansException;
4.  import org.springframework.beans.factory.config.BeanPostProcessor;
5.  import org.springframework.core.Ordered;

7.  public class  InitHelloWorld2 implements BeanPostProcessor, Ordered {
8.  @Override
9.  public Object  postProcessBeforeInitialization(Object  bean, String  beanName) throws BeansException {
10.  System.out.println("B Before : " + beanName);
11.  return bean;
12.  }

14.  @Override
15.  public Object  postProcessAfterInitialization(Object  bean, String  beanName) throws BeansException {
16.  System.out.println("B After : " + beanName);
17.  return bean;
18.  }

20.  @Override
21.  public int getOrder() {
22.  return 0;
23.  }
24.  }

5. 在 src 目录下,创建 Spring 的 XML 配置文件 Beans.xml,配置内容如下。

1.  
2.  

6 .在 net.biancheng.c 包下,创建一个名为 MainApp 的类,代码如下。


1.  package net.biancheng.c;

3.  import org.springframework.context.support.AbstractApplicationContext;
4.  import org.springframework.context.support.ClassPathXmlApplicationContext;

6.  public class  MainApp {
7.  public static void main(String[] args) {
8.  AbstractApplicationContext  context = new ClassPathXmlApplicationContext("Beans.xml");

10.  HelloWorld  obj = (HelloWorld) context.getBean("helloWorld");
11.  obj.getMessage();
12.  context.registerShutdownHook();
13.  }
14.  }

运行结果如下。

B Before : helloWorld
A Before : helloWorld
Bean 正在初始化
B After : helloWorld
A After : helloWorld
Message : Hello World!
Bean 将要被销毁

由运行结果可以看出,postProcessBeforeInitialization 方法是在 Bean 实例化和属性注入后,自定义初始化方法前执行的。而 postProcessAfterInitialization 方法是在自定义初始化方法后执行的。由于 getOrder 方法返回值越大,优先级越低,因此 InitHelloWorld2 先执行。

你可能感兴趣的:(Java架构师,java,spring,java,redis,spring,boot,架构)