1. 程式人生 > 實用技巧 >菜雞的Spring學習—BeanFactoryPostProcessor和BeanPostProcessor

菜雞的Spring學習—BeanFactoryPostProcessor和BeanPostProcessor

BeanPostProcessor

原始碼如下:

public interface BeanPostProcessor {
 
    /**
     * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
     * initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code>
     * 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 * @throws org.springframework.beans.BeansException in case of errors * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
*/ Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; /** * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean * initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code> * 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. * <p>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 <code>bean instanceof FactoryBean</code> checks. * <p>This callback will also be invoked after a short-circuiting triggered by a * {
@link 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 * @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; }

BeanPostProcessor是在spring容器載入了bean的定義檔案並且例項化bean之後執行的,可以在執行bean的初始化方法前後,新增一些自己的處理邏輯。BeanPostProcessor的執行順序是在BeanFactoryPostProcessor之後。

BeanFactoryPostProcessor

原始碼如下:

public interface BeanFactoryPostProcessor {
 
    /**
     * Modify the application context's internal bean factory after its standard
     * initialization. All bean definitions will have been loaded, but no beans
     * will have been instantiated yet. This allows for overriding or adding
     * properties even to eager-initializing beans.
     * @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;
 
}

可以在spring的bean建立之前,修改bean的定義屬性。也就是說,Spring允許BeanFactoryPostProcessor在容器例項化任何其它bean之前讀取配置元資料,並可以根據需要進行修改,例如可以把bean的scope從singleton改為prototype,也可以把property的值給修改掉。BeanFactoryPostProcessor是在spring容器載入了bean的定義檔案之後,在bean例項化之前執行的。