Spring各种PostProcessor : BeanFactoryPostProcessor

概述

顾名思义,BeanFactoryPostProcessor定义了关于 BeanFactoryPostProcessor。这里的BeanFactory其实就是一般常说的Spring bean 容器,通常是一个DefaultListableBeanFactory,它实现了BeanDefinitionRegistry接口用于作为bean定义注册表,同时也实现了接口ConfigurableListableBeanFactory用于作为一个bean容器

执行时机 : 在BeanFactory的标准初始化之后并且所有的BeanDefinitionRegistryPostProcessor执行之后,此时所有的bean定义已经加载但是bean实例尚未创建。

具体调用位置可以参考PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors

源代码解析

/**
 * 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;

}

相关文章

Springboot 应用常见的一些 BeanFactoryPostProcessor

你可能感兴趣的:(spring,Spring,Core)