BeanFactoryPostProcessor & BeanDefinitionRegistryPostProcessor——执行时机

本文介绍了BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor的执行时机。在Spring框架中,这两个接口在加载Bean定义后、实例化Bean之前执行。BeanFactoryPostProcessor先于BeanDefinitionRegistryPostProcessor执行,允许在初始化阶段修改bean定义。在BeanDefinitionRegistryPostProcessor中新增的bean定义,可以被BeanFactoryPostProcessor捕获。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcessor
BeanDefinitionRegistryPostProcessor
BeanFactoryPostProcessor

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

BeanDefinitionRegistryPostProcessor

public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
   
	void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
}

执行时机:抽象的来看就是 BeanFactory加载完Bean定义后,实例化Bean之前执行。
从代码角度就是AbstractApplicationContext#refresh中的invokeBeanFactoryPostProcessors(beanFactory);语句(以下代码的第22行)

	public void refresh() throws BeansException, IllegalStateException {
   
		synchronized (this.startupShutdownMonitor) {
   
			StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

			// Prepare this context for refreshing.
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值