概述
顾名思义,BeanDefinitionRegistryPostProcessor定义了关于BeanDefinitionRegistry 的 PostProcessor 。这里的BeanDefinitionRegistry其实就是一般常说的Spring bean 容器,通常是一个DefaultListableBeanFactory,它实现了BeanDefinitionRegistry接口用于作为bean定义注册表,同时也实现了接口ConfigurableListableBeanFactory用于作为一个bean容器。
执行时机 : 在BeanDefinitionRegistry的标准初始化之后所有其他一般的BeanFactoryPostProcessor执行之前执行,此时所有的bean定义已经加载但是还没有bean实例被创建。
BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcessor, 可以在一般BeanFactoryPostProcessor调用之前对BeanDefinition做一些操作, 尤其是它可以注册用来生成BeanFactoryPostProcessor的bean定义。
具体调用位置可以参考PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors
BeanDefinitionRegistryPostProcessor的一个典型例子可以参考: ConfigurationClassPostProcessor
源代码解析
/**
* 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;
}
本文详细解析了BeanDefinitionRegistryPostProcessor接口的作用与执行时机,它是Spring框架中BeanFactoryPostProcessor的扩展,允许在Bean实例化前注册额外的bean定义,特别是用于生成BeanFactoryPostProcessor的bean定义。
830

被折叠的 条评论
为什么被折叠?



