【五】Spring IoC 最全源码详解之registerBeanPostProcessors

本文详细介绍了Spring中BeanPostProcessor的注册过程,包括其作用、流程详解以及在Spring AOP实现中的关键地位。通过分析`refresh()`方法,解释了如何将BeanPostProcessor实例化并分类,最后注册到bean工厂,以实现对bean实例化过程的干预。
摘要由CSDN通过智能技术生成

目录

1. BeanPostProcessor是什么

2. 流程详解

3.BeanPostProcessor的作用


本节将继续按照refresh()方法中的执行顺序,分析BeanPostProcessor的注册过程。

1. BeanPostProcessor是什么

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

它是Spring提供的一种干预bean实例化过程的一种扩展方式。实现类可以重写postProcessBeforeInitialization和postProcessAfterInitialization方法,以干预bean的实例化结果。介绍BeanPostProcessor具体应用文章很多,读者可以自行搜索,本文主要从源码的角度介绍BeanPostProcessor的注册过程。了解BeanPostProcessor的作用原理,是深入理解Spring AOP实现原理的关键。

postProcessBeforeInitialization 作用于bean实例化方法之后,init初始化方法之前。
postProcessBeforeInitialization 作用于已经bean已经init初始化方法执行完成之后。

2. 流程详解

public void refresh() throws BeansException, IllegalStateException {
    synchronized (this.startupShutdownMonitor) {
        // 为刷新容器前做准备,没什么干货,略过。
        prepareRefresh();

        // 简单理解为获得bean工厂DefaultListableBeanFactory对象即可。
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

        // 为bean工厂加装一些设备,使其在后续繁杂的bean制造过程中能游刃有余
        prepareBeanFactory(beanFactory);

        try {
            // 空壳方法,为以后Spring扩展预留,略过
            postProcessBeanFactory(beanFactory);

            // 对BeanFactoryPostProcessor的注册和回调方法执行
            invokeBeanFactoryPostProcessors(beanFactory);

            // 注册bean的后置处理器,使得bean工厂可以干预bean的实例化过程
            registerBeanPostProcessors(beanFactory);

            // 国际化(略过)
            initMessageSource();

            // 初始化Application事件的广播器
            initApplicationEventMulticaster();

            // Initialize other special beans in specific context subclasses.
            onRefresh();

            // 注册事件监听器
            registerListeners();

            // 实例化non-lazy的singleton bean
            finishBeanFactoryInitialization(beanFactory);

            // Last step: publish corresponding event.
            finishRefresh();
        }
        finally {
            // Reset common introspection caches in Spring's core, since we
            // might not ever need metadata for singleton beans anymore...
            resetCommonCaches();
        }
    }
}

refresh()-->registerBeanPostProcessors(beanFactory)-->PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this)。跳过流程中的包装方法,直接进入干活的最终方法。

public static void registerBeanPostProcessors(
        ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值