Spring自問自答系列1----BeanFactoryPostProcessor與BeanPostProcessor的區別

BeanFactoryProcessorBeanPostProcessor貌似能夠控制Bean的實例化過程,本文將講解二者的概念和區別。java

什麼是BeanFactoryPostProcessor

BeanFactoryPostProcessor源代碼以下:spring

public interface BeanFactoryPostProcessor {
    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}

BeanFactoryPostProcessor是對BeanFactory級別的處理,是針對整個Bean的工廠進行處理。 若是使用自定義BeanFactoryProcessor,執行路徑以下:app

org.springframework.context.support.AbstractApplicationContext#refresh
    org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors
        org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.List<org.springframework.beans.factory.config.BeanFactoryPostProcessor>)
            org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(java.util.Collection<? extends org.springframework.beans.factory.config.BeanFactoryPostProcessor>, org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
                org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory
                    com.jackwu.spring.config.customBeanFactoryPostProcessor#postProcessBeanFactory

什麼是BeanPostProcessor

BeanPostProcessor源代碼以下:post

public interface BeanPostProcessor {
	Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
	Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}

postProcessBeforeInitialization方法做用於bean初始化以前,postProcessBeforeInitialization方法做用於bean初始化以後。code

postProcessBeforeInitialization執行流程以下:接口

org.springframework.context.support.AbstractApplicationContext#refresh
    org.springframework.context.support.AbstractApplicationContext#finishBeanFactoryInitialization
        org.springframework.beans.factory.config.ConfigurableListableBeanFactory#preInstantiateSingletons
            org.springframework.beans.factory.support.AbstractBeanFactory#getBean(java.lang.String)
                org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
                    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#getSingleton(java.lang.String, org.springframework.beans.factory.ObjectFactory<?>)
                        org.springframework.beans.factory.ObjectFactory#getObject
                            org.springframework.beans.factory.support.AbstractBeanFactory#createBean
                                org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
                                    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)
                                        org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInitialization
                                          org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization

AbstractAutowireCapableBeanFactory.java關鍵代碼以下:ip

Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
    // 執行postProcessBeforeInitialization
	wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}

try {
	invokeInitMethods(beanName, wrappedBean, mbd);
} catch (Throwable ex) {
	throw new BeanCreationException(
	(mbd != null ? mbd.getResourceDescription() : null),
		beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
    // 執行postProcessAfterInitialization
    wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}

二者區別

以上兩種都爲Spring提供的後處理bean的接口,只是二者執行的時機不同。前者爲實例化(instantialize)以後,後者是實例化以前。功能上,後者對bean的處理功能更增強大。get

相關文章
相關標籤/搜索