BeanFactoryPostProcessor与BeanPostProcessor区别

Spring IoC容器允许BeanFactoryPostProcessor在容器实例化任何bean之前读取bean的定义(BeanDefinition),并可以修改它(参见Spring的
PropertyResourceConfigurer源码
)。同时可以定义多个BeanFactoryPostProcessor,通过设置'order'属性来确定各个BeanFactoryPostProcessor执行顺序。'order'值越小优先级越高。

注册一个BeanFactoryPostProcessor实例需要定义一个Java类来实现BeanFactoryPostProcessor接口,并重写该接口的postProcessorBeanFactory方法。通过beanFactory可以获取bean的定义信息,并可以修改bean的定义信息。这点是和BeanPostProcessor最大区别

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中内置了一些BeanFactoryPostProcessor实现类:

BeanFactoryPostProcessor与BeanPostProcessor区别

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
灡玉子的头像 - 鹿快
评论 共1条

请登录后发表评论