Spring - BeanPostProcessor接口(后处理器)讲解

概述:

BeanPostProcessor接口是众多Spring提供给开发者的bean生命周期内自定义逻辑拓展接口中的一个,其他还有类似InitializingBean,DisposableBean,BeanFactoryAware等。
实现了BeanPostProcessor接口的Bean我们叫做后处理器
 

BeanPostProcessor接口定义如下

<span style="font-size:14px;">public interface BeanPostProcessor {

       Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;

       Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

}</span>

在项目中(假设只有一个Spring容器),我们如果定义了一个实现了BeanPostProcessor接口的Bean(后处理器),那么在这个Bean所在的容器中的其他所有Bean在初始化前都会执行后处理器的postProcessBeforeInitialization方法,在初始化后都会执行后处理器的postProcessAfterInitialization方法。具体执行顺序是:
1. BeanPostProcessor.postProcessBeforeInitialization
2. InitializingBean.afterPropertiesSet
3. bean配置中的init-method
4. BeanPostProcessor.postProcessAfterInitialization
关于bean生命周期可以参考这里

一个容器中可以定义多个后处理器,那么该容器内其他素有Bean初始化前后就会依次执行这些Bean的postProcessBeforeInitialization方法和postProcessAfterInitialization方法。

后处理器的配置方式和普通的Spring Bean是一样的。

下面我们来看一个Spring的例子:

类定义:
package scripting;

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

public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor {

  // simply return the instantiated bean as-is
  public Object postProcessBeforeInitialization(Object bean, String beanName)
                                                                     throws BeansException {
      return bean; // we could potentially return any object reference here...
  }

  public Object postProcessAfterInitialization(Object bean, String beanName)
                                                                     throws BeansException {
      System.out.println("Bean '" + beanName + "' created : " + bean.toString());
      return bean;
  }
}
spring配置: 
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:lang="http://www.springframework.org/schema/lang"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/lang
         http://www.springframework.org/schema/lang/spring-lang.xsd">

  <lang:groovy id="messenger"
        script-source="classpath:org/springframework/scripting/groovy/Messenger.groovy">
      <lang:property name="message" value="Fiona Apple Is Just So Dreamy."/>
  </lang:groovy>

  <!--
      when the above bean (messenger) is instantiated, this custom
      BeanPostProcessor implementation will output the fact to the system console
   -->
  <bean class="scripting.InstantiationTracingBeanPostProcessor"/>

</beans>
 
配置中的messenger也是一个bean,具体可以参考官方资料Chapter 28, Dynamic language support,这里就不细说。
写一个main方法启动Spring容器:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scripting.Messenger;

public final class Boot {

  public static void main(final String[] args) throws Exception {
      ApplicationContext ctx = new ClassPathXmlApplicationContext("scripting/beans.xml");
      Messenger messenger = (Messenger) ctx.getBean("messenger");
      System.out.println(messenger);
  }
}
执行后,打印结果如下:
Bean 'messenger' created : org.springframework.scripting.groovy.GroovyMessenger@272961
org.springframework.scripting.groovy.GroovyMessenger@272961

在配置中,实现了BeanPostProcessor接口的Bean(后处理器)会优先被Spring容器实例化,以便在后续实例化其他Bean的时候能起作用。

 

posted @ 2015-11-17 10:38  影子zZzZ  阅读(2424)  评论(0编辑  收藏  举报