spring源码04——AbstractApplicationContext.registerBeanPostProcessors

类名:AbstractApplicationContext

protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
		//先获取容器所有的BeanPostProcessor
        String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
        int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
        beanFactory.addBeanPostProcessor(new AbstractApplicationContext.BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
        List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList();
        List<BeanPostProcessor> internalPostProcessors = new ArrayList();
        List<String> orderedPostProcessorNames = new ArrayList();
        List<String> nonOrderedPostProcessorNames = new ArrayList();
        String[] arr$ = postProcessorNames;
        int len$ = postProcessorNames.length;

        String ppName;
        BeanPostProcessor pp;
        for(int i$ = 0; i$ < len$; ++i$) {
            ppName = arr$[i$];
			//首先添加实现接口PriorityOrdered的BeanPostProcessor
            if (this.isTypeMatch(ppName, PriorityOrdered.class)) {
                pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
                priorityOrderedPostProcessors.add(pp);
                if (pp instanceof MergedBeanDefinitionPostProcessor) {
                    internalPostProcessors.add(pp);
                }
				//然后添加实现Ordered接口的BeanPostProcessor
            } else if (this.isTypeMatch(ppName, Ordered.class)) {
                orderedPostProcessorNames.add(ppName);
            } else {
			//最后添加没有实现优先级接口的BeanPostProcessor
                nonOrderedPostProcessorNames.add(ppName);
            }
        }
		//对priorityOrderedPostProcessors进行排序
        OrderComparator.sort(priorityOrderedPostProcessors);
        this.registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
        List<BeanPostProcessor> orderedPostProcessors = new ArrayList();
        Iterator i$ = orderedPostProcessorNames.iterator();

        while(i$.hasNext()) {
            String ppName = (String)i$.next();
			//通过getBean()方法初始化BeanPostProcessor
            BeanPostProcessor pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
            orderedPostProcessors.add(pp);
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }
        }

        OrderComparator.sort(orderedPostProcessors);
        this.registerBeanPostProcessors(beanFactory, orderedPostProcessors);
        List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList();
        Iterator i$ = nonOrderedPostProcessorNames.iterator();

        while(i$.hasNext()) {
            ppName = (String)i$.next();
            pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
            nonOrderedPostProcessors.add(pp);
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }
        }

        this.registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
        OrderComparator.sort(internalPostProcessors);
        //保存到beanFactory中
        this.registerBeanPostProcessors(beanFactory, internalPostProcessors);
        beanFactory.addBeanPostProcessor(new AbstractApplicationContext.ApplicationListenerDetector());
    }

1、 String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

先获取容器所有的BeanPostProcessor

2、if (this.isTypeMatch(ppName, PriorityOrdered.class))

首先添加实现接口PriorityOrdered的BeanPostProcessor

3、else if (this.isTypeMatch(ppName, Ordered.class))

然后添加实现Ordered接口的BeanPostProcessor

4、this.registerBeanPostProcessors(beanFactory, internalPostProcessors);

保存到beanFactory中

5、pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);

spring源码05——AbstractBeanFactory.doGetBean
调用链:
BeanPostProcessor pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
AbstractBeanFactory.getBean(String name, Class requiredType);
AbstractBeanFactory.doGetBean(String name, Class requiredType, final Object[] args, boolean typeCheckOnly)

你可能感兴趣的:(四,spring,spring)