Spring中的BeanPostProcessor接口

Spring提供了不少扩展接口,BeanPostProcessor接口和InstantiationAwareBeanPostProcessor接口就是其中两个。javascript


BeanPostProcessor

BeanPostProcessor接口做用是:若是咱们须要在Spring容器完成Bean的实例化、配置和其余的初始化先后添加一些本身的逻辑处理,咱们就能够定义一个或者多个BeanPostProcessor接口的实现,而后注册到容器中。
java

Spring中Bean的实例化过程图示:spring


由上图能够看到,Spring中的BeanPostProcessor在实例化过程处于的位置,BeanPostProcessor接口有两个方法须要实 现:postProcessBeforeInitialization和 postProcessAfterInitialization,BeanPostProcessor接口定义以下:express

[javascript] view plaincopyapp

  1. package org.springframework.beans.factory.config;  post

  2.   

  3. import org.springframework.beans.BeansException;  ui

  4.   

  5. public interface BeanPostProcessor {  this

  6.   

  7.     Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;  spa

  8.   

  9.     Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;  .net

  10. }  


由方法名字也能够看出,前者在实例化及依赖注入完成后、在任何初始化代码(好比配置文件中的init-method)调用以前调用;后者在初始化代码调用以后调用。

注意:

一、接口中的两个方法都要将传入的bean返回,而不能返回null,若是返回的是null那么咱们经过getBean方法将得不到目标。

二、BeanFactory和ApplicationContext对待bean后置处理器稍有不一样。ApplicationContext会自动检测在 配置文件中实现了BeanPostProcessor接口的全部bean,并把它们注册为后置处理器,而后在容器建立bean的适当时候调用它,所以部署 一个后置处理器同部署其余的bean并无什么区别。而使用BeanFactory实现的时候,bean 后置处理器必须经过代码显式地去注册,在IoC容器继承体系中的ConfigurableBeanFactory接口中定义了注册方法:

[java] view plaincopy

  1. /** 
     * Add a new BeanPostProcessor that will get applied to beans created 
     * by this factory. To be invoked during factory configuration. 
     * <p>Note: Post-processors submitted here will be applied in the order of 
     * registration; any ordering semantics expressed through implementing the 
     * {@link org.springframework.core.Ordered} interface will be ignored. Note 
     * that autodetected post-processors (e.g. as beans in an ApplicationContext) 
     * will always be applied after programmatically registered ones. 
     * @param beanPostProcessor the post-processor to register 
     */  
    void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);

另外,不要将BeanPostProcessor标记为延迟初始化。由于若是这样作,Spring容器将不会注册它们,自定义逻辑也就没法获得应用。假如 你在<beans />元素的定义中使用了'default-lazy-init'属性,请确信你的各个BeanPostProcessor标记为'lazy- init="false"'。


InstantiationAwareBeanPostProcessor

InstantiationAwareBeanPostProcessor是BeanPostProcessor的子接口,能够在Bean生命周期的另外 两个时期提供扩展的回调接口,即实例化Bean以前(调用postProcessBeforeInstantiation方法)和实例化Bean以后(调 用postProcessAfterInstantiation方法),该接口定义以下:

[java] view plaincopy

  1. package org.springframework.beans.factory.config;  
      
    import java.beans.PropertyDescriptor;  
      
    import org.springframework.beans.BeansException;  
    import org.springframework.beans.PropertyValues;  
      
    public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {  
      
        Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException;  
      
        boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException;  
      
        PropertyValues postProcessPropertyValues(  
                PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)  
                throws BeansException;  
      
    }

其使用方法与上面介绍的BeanPostProcessor接口相似,只时回调时机不一样。

相关文章
相关标签/搜索