Spring源码情操陶冶-AbstractApplicationContext#registerBeanPostProcessors

承接前文Spring源码情操陶冶-AbstractApplicationContext#invokeBeanFactoryPostProcessors

瞧瞧官方注释

    /**
     * Instantiate and invoke all registered BeanPostProcessor beans,
     * respecting explicit order if given.
     * <p>Must be called before any instantiation of application beans.
     */

实例化并且调用所有的已注册的BeanPostProcessor beans,其实也就是简单的把DefaultListableFactory中的所有为BeanPostProcessor的实现bean类放置于其内部属性beanPostProcessors List集合中。

直接源码

protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    //调用的是委托类的registerBeanPostProcessors方法      
    PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
    }

间接源码-PostProcessorRegistrationDelegate.registerBeanPostProcessors

public static void registerBeanPostProcessors(
            ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
//获取beanFactory中的所有实现BeanPostProcessor接口的bean
        String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

        // Register BeanPostProcessorChecker that logs an info message when
        // a bean is created during BeanPostProcessor instantiation, i.e. when
        // a bean is not eligible for getting processed by all BeanPostProcessors.
        int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
        //BeanPostProcessorChecker检查类
        beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

        //此处的实现与上节的invokeBeanFactoryPostProcessors方法一致,这里就省略不赘述了
        List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
        List<BeanPostProcessor> internalPostProcessors = new ArrayList<BeanPostProcessor>();
        List<String> orderedPostProcessorNames = new ArrayList<String>();
        List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
        /**省略*/

        // Finally, re-register all internal BeanPostProcessors.
        OrderComparator.sort(internalPostProcessors);
        //主要是调用此方法将所有的postProcessors按照优先级有序的放置在beanFactory的内部属性beanPostProcessors集合中
        registerBeanPostProcessors(beanFactory, internalPostProcessors);
        //增加ApplicationListenerDetector类
        beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
    }

小结

  1. 获取beanFactory中的所有实现postProcessor接口的bean对象按照优先级有序的放置在beanFactory的内部属性beanPostProcessors集合中,但是并没有执行其中的相应公共方法
  2. 新增两个BeanPostProcessor实现类:BeanPostProcessorChecker为首,ApplicationListenerDetector为尾

下节预告

Spring源码情操陶冶-AbstractApplicationContext#initMessageSource

转载于:https://www.cnblogs.com/question-sky/p/6825588.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值