Spring擴(kuò)展功能BeanDefinitionRegistryPostProcessor源碼解讀

前面我們對BeanDefinitionRegistryPostProcessor做了簡單的學(xué)習(xí),在創(chuàng)建bean之前我們可以按照自己的意愿可以向容器中注冊我們自己的bean信息,本節(jié)我們來深入學(xué)習(xí)它的相關(guān)源碼知識,分析的入口還是我們的IOC容器的創(chuàng)建過程,在上節(jié)BeanFactoryPostProcessor中我們已經(jīng)提到了,這里直接從AbstractApplicatContext的#refresh()方法來看

在#refresh()方法里如下代碼片段需要我們注意一下:

  this.invokeBeanFactoryPostProcessors(beanFactory);

跟蹤代碼來到AbstractApplicationContext#invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory)方法中

 protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, this.getBeanFactoryPostProcessors());
    if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean("loadTimeWeaver")) {
        beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
        beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    }

}

上述代碼片段中PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, this.getBeanFactoryPostProcessors())需要我們值得注意,Dbug進(jìn)去來到:PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) 方法,所有的操作在此方法中完成:

    1. 首先從容器中獲取到類型為【BeanDefinitionRegistryPostProcessor】的組件
    1. 依次調(diào)用#postProcessBeanDefinitionRegistry(...)方法
    1. 接著是調(diào)用#postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)方法
  • 4 .最后找到類型為BeanFactoryPostProcessor的組件來觸發(fā)#postProcessBeanFactory()

上述就是關(guān)于BeanDefinitionRegistryPostProcessor相關(guān)源碼的解讀,具體細(xì)節(jié)自己感興趣的可以去看看

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容