Spring的各种PostProcessor

来源:互联网 发布:java array contains 编辑:程序博客网 时间:2024/06/03 19:26

简介 : Spring 的几种 PostProcessor

Spring 有如下几种PostProcessor :
1. BeanDefinitionRegistryPostProcessor
2. BeanFactoryPostProcessor
3. BeanPostProcessor

BeanDefinitionRegistryPostProcessor

BeanDefinitionRegistry 的PostProcessor执行时机 : 在BeanDefinitionRegistry的标准初始化之后所有其他一般的BeanFactoryPostProcessor执行之前执行,此时所有的bean定义已经加载但是bean尚未创建。BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的扩展, 可以在一般BeanFactoryPostProcessor调用之前对BeanDefinition做一些操作, 尤其是它可以注册用来生成BeanFactoryPostProcessor的bean定义.
/** * Extension to the standard BeanFactoryPostProcessor SPI, allowing for * the registration of further bean definitions before regular * BeanFactoryPostProcessor detection kicks in. In particular, * BeanDefinitionRegistryPostProcessor may register further bean definitions * which in turn define BeanFactoryPostProcessor instances. *  * 标准BeanFactoryPostProcessor SPI的扩展,允许在常规BeanFactoryPostProcessor 检测 * 开始前注册更多的bean定义。尤其值得一提的是,BeanDefinitionRegistryPostProcessor  * 甚至可以注册用来定义BeanFactoryPostProcessor实例的bean定义。 *  * 注意:该接口继承了另外一个接口 BeanFactoryPostProcessor  *  * @作者 Juergen Hoeller * @开始版本 3.0.1 * @参考 org.springframework.context.annotation.ConfigurationClassPostProcessor */public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {    /**     * Modify the application context's internal bean definition registry after its     * standard initialization.      * 在应用上下文内部的bean definition registry的标准初始化之后修改对其进行修改。     *      * All regular bean definitions will have been loaded,     * but no beans will have been instantiated yet.      *      * 此时所有常规的bean定义已经被加载,但是还没有bean被实例化。     *      * This allows for adding further     * bean definitions before the next post-processing phase kicks in.     *      * 这样可以在下一阶段post-processing触发之前增加更多的bean定义。     *      * @param registry the bean definition registry used by the application context     * @throws org.springframework.beans.BeansException in case of errors     */    void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;}

BeanFactoryPostProcessor

BeanFactory 的PostProcessor执行时机 : 在BeanFactory 的标准初始化之后所有的BeanDefinitionRegistryPostProcessor执行之后执行,此时所有的bean定义已经加载但是bean尚未创建。
/** * Allows for custom modification of an application context's bean definitions, * adapting the bean property values of the context's underlying bean factory. *  * 1. 允许对上下文的bean定义做定制化修改; * 2. 调整上下文内部bean factory中bean的属性值; *  * Application contexts can auto-detect BeanFactoryPostProcessor beans in * their bean definitions and apply them before any other beans get created. *   * 应用上下文能够从它的bean定义中自动检测BeanFactoryPostProcessor bean,并在其他任何 * bean创建之前应用这些BeanFactoryPostProcessor。 * * Useful for custom config files targeted at system administrators that * override bean properties configured in the application context. *  * See PropertyResourceConfigurer and its concrete implementations * for out-of-the-box solutions that address such configuration needs. * * A BeanFactoryPostProcessor may interact with and modify bean * definitions, but never bean instances. Doing so may cause premature bean * instantiation, violating the container and causing unintended side-effects. * If bean instance interaction is required, consider implementing * BeanPostProcessor instead. *  * BeanFactoryPostProcessor操作bean定义上而不是bean实例。 * 如果需要操作bean实例,考虑实现BeanPostProcessor接口。 *  * @author Juergen Hoeller * @since 06.07.2003 * @see BeanPostProcessor * @see PropertyResourceConfigurer */public interface BeanFactoryPostProcessor {    /**     * Modify the application context's internal bean factory after its standard     * initialization.      *       * 在bean factory标准初始化之后对其进行修改。     *      * All bean definitions will have been loaded, but no beans     * will have been instantiated yet.      *      * 此时所有bean定义已经被加载,但是还没有bean被实例化。     *      * This allows for overriding or adding properties even to eager-initializing beans.     *       * 对bean的属性进行重写或者增加修改,甚至对eager-initializing bean也生效。     *      * @param beanFactory the bean factory used by the application context     * @throws org.springframework.beans.BeansException in case of errors     */    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;}

BeanPostProcessor

执行时机 : 在容器加载了bean定义并且实例化bean之后执行
/** * Factory hook that allows for custom modification of new bean instances, * e.g. checking for marker interfaces or wrapping them with proxies. *  * 对新的bean实例进行订制化修改的factory hook。 * ApplicationContexts can autodetect BeanPostProcessor beans in their * bean definitions and apply them to any beans subsequently created. *  * 应用程序上下文ApplicationContext能够从自己的bean定义中自动检测BeanPostProcessor bean, * 并随后在任意bean创建时应用到新创建的bean。 *  * Plain bean factories allow for programmatic registration of post-processors, * applying to all beans created through this factory. * * Typically, post-processors that populate beans via marker interfaces * or the like will implement #postProcessBeforeInitialization, * while post-processors that wrap beans with proxies will normally * implement #postProcessAfterInitialization. * * @author Juergen Hoeller * @since 10.10.2003 * @see InstantiationAwareBeanPostProcessor * @see DestructionAwareBeanPostProcessor * @see ConfigurableBeanFactory#addBeanPostProcessor * @see BeanFactoryPostProcessor */public interface BeanPostProcessor {    /**     * 执行时机 :      * bean实例创建==>填充属性值==>postProcessBeforeInitialization()==>初始化回调执行     * 这里初始化回调指的是以下几种情况 :     *  1. 通过InitializingBean接口实现的afterPropertiesSet方法;     *  2. xml 方式指定的bean的 init-method 初始化方法;     *  3. JSR-250 注解 @PostConstruct 注解的初始化方法;     *  4. Java 配置中 @Bean(initMethod = "init") 指定的初始化方法;     *      * Apply this BeanPostProcessor to the given new bean instance before any bean     * initialization callbacks (like InitializingBean's afterPropertiesSet     * or a custom init-method). The bean will already be populated with property values.     * The returned bean instance may be a wrapper around the original.     *      *      * @param bean the new bean instance     * @param beanName the name of the bean     * @return the bean instance to use, either the original or a wrapped one;     * if null, no subsequent BeanPostProcessors will be invoked     * @throws org.springframework.beans.BeansException in case of errors     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet     */    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;    /**     * 执行时机 :      * bean实例创建=>填充属性值=>初始化回调执行=>postProcessAfterInitialization()     * 这里初始化回调指的是以下几种情况 :     *  1. 通过InitializingBean接口实现的afterPropertiesSet方法;     *  2. xml 方式指定的bean的 init-method 初始化方法;     *  3. JSR-250 注解 @PostConstruct 注解的初始化方法;     *  4. Java 配置中 @Bean(initMethod = "init") 指定的初始化方法;     *      * Apply this BeanPostProcessor to the given new bean instance after any bean     * initialization callbacks (like InitializingBean's afterPropertiesSet     * or a custom init-method). The bean will already be populated with property values.     * The returned bean instance may be a wrapper around the original.     *      * In case of a FactoryBean, this callback will be invoked for both the FactoryBean     * instance and the objects created by the FactoryBean (as of Spring 2.0). The     * post-processor can decide whether to apply to either the FactoryBean or created     * objects or both through corresponding bean instanceof FactoryBean checks.     *      * This callback will also be invoked after a short-circuiting triggered by a     * InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation method,     * in contrast to all other BeanPostProcessor callbacks.     *      * @param bean the new bean instance     * @param beanName the name of the bean     * @return the bean instance to use, either the original or a wrapped one;     * if null, no subsequent BeanPostProcessors will be invoked     * @throws org.springframework.beans.BeansException in case of errors     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet     * @see org.springframework.beans.factory.FactoryBean     */    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;}