Spring源码解析(三)-BeanPostProcessor的实例化过程

AbstractApplicationContext - 352 - N201
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    	//N202  处理了BeanDefinitionRegistryPostProcessor接口实现类具体过程
        PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, this.getBeanFactoryPostProcessors());
        if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean("loadTimeWeaver")) {
            beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
            beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
        }

    }
PostProcessorRegistrationDelegate - 34 - N202
public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
    	//这个容器存的是已经被实例化过得BeanDefinitionRegistryPostProcessor的名字name,避免重复调用
        Set<String> processedBeans = new HashSet();
    	//这个容易存参数beanFactoryPostProcessors中父类属于BeanFactoryPostProcess的对象
        ArrayList regularPostProcessors;
    	//这个容器是存所有的BeanDefinitionRegistryPostProcessor类型bean实例的
        ArrayList registryProcessors;
        int var9;
        ArrayList currentRegistryProcessors;
        String[] postProcessorNames;
        if (beanFactory instanceof BeanDefinitionRegistry) {
            BeanDefinitionRegistry registry = (BeanDefinitionRegistry)beanFactory;
            regularPostProcessors = new ArrayList();
            registryProcessors = new ArrayList();
            Iterator var6 = beanFactoryPostProcessors.iterator();

            while(var6.hasNext()) {
                BeanFactoryPostProcessor postProcessor = (BeanFactoryPostProcessor)var6.next();
                if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                    BeanDefinitionRegistryPostProcessor registryProcessor = (BeanDefinitionRegistryPostProcessor)postProcessor;
                    registryProcessor.postProcessBeanDefinitionRegistry(registry);
                    registryProcessors.add(registryProcessor);
                } else {
                    regularPostProcessors.add(postProcessor);
                }
            }
            //这个容器是存每一步当前的BeanDefinitionRegistryPostProcessor实例的容器   
            //从这里开始就是对BeanDefinitionRegistryPostProcessor这个接口实现类的实例化并且调用的过程。
            currentRegistryProcessors = new ArrayList();
            //首先获取实现了BeanDefinitionRegistryPostProcessor接口的所有类的BeanDefinition对象的beanName
            postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            String[] var16 = postProcessorNames;
            var9 = postProcessorNames.length;

            int var10;
            String ppName;
            for(var10 = 0; var10 < var9; ++var10) {
                ppName = var16[var10];
                //遍历postProcessorNames这个容器,判断是否实现了PriorityOrdered接口
                if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                    //第一种,将实现了PriorityOrdered排序类的BeanDefinitionRegistryPostProcessor实例化并放入容器
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    //每个BeanDefinitionRegistryPostProcessor都只要处理一次,所以需要将正在处理的
                    //BeanDefinitionRegistryPostProcessor存入processedBeans容器,避免后续重复调用。
                    processedBeans.add(ppName);
                }
            }
			//排序
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            //将BeanDefinitionRegistryPostProcessor实例全部添加到registryProcessors这个容器,现在这个容器有一种情况实例
            registryProcessors.addAll(currentRegistryProcessors);
            //N203   调用接口实现方法
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
            //清空currentRegistryProcessors这个存储当前处理的BeanDefinitionRegistryPostProcessor容器,准备进入下一种
            currentRegistryProcessors.clear();
            //获取实现了BeanDefinitionRegistryPostProcessor接口的所有类的BeanDefinition对象的beanName
            postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            var16 = postProcessorNames;
            var9 = postProcessorNames.length;

            for(var10 = 0; var10 < var9; ++var10) {
                ppName = var16[var10];
                //遍历postProcessorNames这个容器,判断是否实现了Ordered接口,并且还需要判断是否在processdBeans容器中,
                //如果存在则说明前面处理过,直接跳过避免重复处理
                if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
                    //第二种,将实现了Ordered排序类的BeanDefinitionRegistryPostProcessor实例化并放入容器,这个容器上一	
                    //种结束已经清空
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    //每个BeanDefinitionRegistryPostProcessor都只要处理一次,所以需要将正在处理的
                    //BeanDefinitionRegistryPostProcessor存入容器,避免后续重复调用。
                    processedBeans.add(ppName);
                }
            }
			//排序
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            //将BeanDefinitionRegistryPostProcessor实例全部追加到registryProcessors这个容器,现在这容器就有两种情况实例
            registryProcessors.addAll(currentRegistryProcessors);
            //N203   调用接口实现方法
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
            //清空currentRegistryProcessors这个存储当前处理的BeanDefinitionRegistryPostProcessor容器,准备进入下一种
            currentRegistryProcessors.clear();
            
            //没实现排序接口的
            boolean reiterate = true;
            while(reiterate) {
                reiterate = false;
                //获取实现了BeanDefinitionRegistryPostProcessor接口的所有类的BeanDefinition对象的beanName
                postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
                String[] var19 = postProcessorNames;
                var10 = postProcessorNames.length;

                for(int var26 = 0; var26 < var10; ++var26) {
                    String ppName = var19[var26];
                    //判断是否在processdBeans容器中,如果存在则说明前面处理过,直接跳过避免重复处理
                    if (!processedBeans.contains(ppName)) {
                        //第三种,没有实现排序类的BeanDefinitionRegistryPostProcessor实例化并放入容器,
                    	//这个容器上一种结束已经清空
                        currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                        //每个BeanDefinitionRegistryPostProcessor都只要处理一次,所以需要将正在处理的
                        //BeanDefinitionRegistryPostProcessor存入容器,避免后续重复调用。
                        processedBeans.add(ppName);
                        reiterate = true;
                    }
                }
				//排序
                sortPostProcessors(currentRegistryProcessors, beanFactory);
                //将BeanDefinitionRegistryPostProcessor实例全部追加到registryProcessors这个容器,现在这容器就有所有实例
                registryProcessors.addAll(currentRegistryProcessors);
                 //N203   调用BeanDefinitionRegistryPostProcessor接口实现方法
                invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
                //清空currentRegistryProcessors这个存储当前处理的BeanDefinitionRegistryPostProcessor容器
                currentRegistryProcessors.clear();
            }
            //这里的registryProcessors这个容器经过上面的步骤,就会存有所有的BeanDefinitionRegistryPostProcessor这个类实
            //例,而BeanDefinitionRegistryPostProcessor这个类继承自BeanFactoryPostProcessor,BeanFactoryPostProcessor
            //这个类又有一个postProcessBeanFactory方法,所以这里也需要调用这些实例的这个方法。
            //N204   调用BeanFactoryPostProcessor接口实现方法postProcessBeanFactory方法
            invokeBeanFactoryPostProcessors((Collection)registryProcessors, (ConfigurableListableBeanFactory)beanFactory);
            invokeBeanFactoryPostProcessors((Collection)regularPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
        } else {
            invokeBeanFactoryPostProcessors((Collection)beanFactoryPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
        }
	//上面是处理实现BeanDefinitionRegistryPostProcessor这个类的bean,而BeanDefinitionRegistryPostProcessor又继承自
    //BeanFactoryPostProcessor这个类。那么接下来就是处理直接实现BeanFactoryPostProcessor这个类的bean
    	//获取实现了BeanFactoryPostProcessor接口的所有类的BeanDefinition对象的beanName
        String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
    	//存实现了PriorityOrdered接口的BeanFactoryPostProcessor接口的实例
        regularPostProcessors = new ArrayList();
    	//存实现了Ordered接口的BeanFactoryPostProcessor接口的实现类的bd对象的beanName
        registryProcessors = new ArrayList();
    	//存没有实现排序接口的BeanFactoryPostProcessor接口的实现类的bd对象的beanName
        currentRegistryProcessors = new ArrayList();
        postProcessorNames = postProcessorNames;
        int var20 = postProcessorNames.length;

        String ppName;
        for(var9 = 0; var9 < var20; ++var9) {
            //遍历所有实现了BeanFactoryPostProcessor接口的类
            ppName = postProcessorNames[var9];
            //processedBeans这个容器是实现了BeanDefinitionRegistryPostProcessor的类,上面处理过了这里要避免重复处理。
            if (!processedBeans.contains(ppName)) {
                if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                    //实现了PriorityOrdered接口,实例化并存入容器
                    regularPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
                } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
                    //实现了Ordered接口的,name存入容器
                    registryProcessors.add(ppName);
                } else {
                    //没实现接口的,name存入容器
                    currentRegistryProcessors.add(ppName);
                }
            }
        }
		//排序实现了PriorityOrdered接口的实例
        sortPostProcessors(regularPostProcessors, beanFactory);
    	//N204   调用BeanFactoryPostProcessor接口实现方法
        invokeBeanFactoryPostProcessors((Collection)regularPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
    	//这个容器存实现了Ordered接口和BeanFactoryPostProcessor接口两个接口的类的实例
        List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList();
        Iterator var21 = registryProcessors.iterator();
        while(var21.hasNext()) {
            //遍历postProcessorName容器(存存实现了Ordered接口的BeanFactoryPostProcessor接口的实现类的bd对象的beanName)
            String postProcessorName = (String)var21.next();
            //调用getBean获取实例并加入到容器中,这里多处用到了getBean方法,
            //而这个方法也是我们整个spring流程中最重要的一个方法之一,后面会有专门的章节讲解。
            orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
        }
		//排序实现了Ordered接口的实例
        sortPostProcessors(orderedPostProcessors, beanFactory);
    	//N204   调用BeanFactoryPostProcessor接口实现方法
        invokeBeanFactoryPostProcessors((Collection)orderedPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
    	//这个容器存没有实现排序接口而实现了BeanFactoryPostProcessor接口的类的实例
        List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList();
        Iterator var24 = currentRegistryProcessors.iterator();

        while(var24.hasNext()) {
            //遍历currentRegistryProcessors容器
            ppName = (String)var24.next();
            //调用getBean获取实例并放入容器
            nonOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
        }
		//N204   调用BeanFactoryPostProcessor接口实现方法
        invokeBeanFactoryPostProcessors((Collection)nonOrderedPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
        beanFactory.clearMetadataCache();
    }
PostProcessorRegistrationDelegate - 242 - N203
private static void invokeBeanDefinitionRegistryPostProcessors(
			Collection<? extends BeanDefinitionRegistryPostProcessor> postProcessors, BeanDefinitionRegistry registry) {
		//遍历容器,调用每个实例的postProcessBeanDefinitionRegistry方法,这个方法是接口的抽象方法,在实现类中可以通过重写
    	//该方法拿到registry,然后自己修改和注册beanDefinition
        Iterator var2 = postProcessors.iterator();
        while(var2.hasNext()) {
            BeanDefinitionRegistryPostProcessor postProcessor = (BeanDefinitionRegistryPostProcessor)var2.next();
            postProcessor.postProcessBeanDefinitionRegistry(registry);
        }
	}

PostProcessorRegistrationDelegate - 252 - N204
private static void invokeBeanFactoryPostProcessors(
			Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {
		//遍历容器,调用每个实例的postProcessBeanFactory方法,这个方法是接口的抽象方法,在实现类中可以通过重写该方法拿到
    	//beanFactory,然后再方法中通过这个beanFactory就可以操作所有的bean了
    	Iterator var2 = postProcessors.iterator();
        while(var2.hasNext()) {
            BeanFactoryPostProcessor postProcessor = (BeanFactoryPostProcessor)var2.next();
            postProcessor.postProcessBeanFactory(beanFactory);
        }
	}

上面是对BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor两个接口的实例化和调用,接下来主要实现了BeanPostProcessor接口的类实例化,并且加入到BeanFactory中,思路也是比较相似的。

AbstractApplicationContext - 361 - N205
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    	//N206   把实现了BeanPostProcessor接口的类实例化,并且加入到BeanFactory中
        PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
    }

PostProcessorRegistrationDelegate - 167 - N206
public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
    	//拿到工程里面所有实现了BeanPostProcessor接口的类,获取到BeanDefinition的名称
        String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
        int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
        beanFactory.addBeanPostProcessor(new PostProcessorRegistrationDelegate.BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
    	//存实现了BeanPostProcessor接口和priorityOrdered接口两个接口的类的实例
        List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList();
    	//存实现了MergedBeanDefinitionPostProcessor接口的类的实例
        List<BeanPostProcessor> internalPostProcessors = new ArrayList();
    	//存实现了BeanPostProcessor接口和Ordered接口两个接口的类的name
        List<String> orderedPostProcessorNames = new ArrayList();
    	//存实现了BeanPostProcessor接口的类的name
        List<String> nonOrderedPostProcessorNames = new ArrayList();
        String[] var8 = postProcessorNames;
        int var9 = postProcessorNames.length;

        String ppName;
        BeanPostProcessor pp;
        for(int var10 = 0; var10 < var9; ++var10) {
            ppName = var8[var10];
            //实现了PriorityOrdered接口的BeanPostProcessor
            if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                //getBean是实例化方法,后面会有专门的章节讲解
                pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
                //将实例存入到容器priorityOrderedPostProcessors
                priorityOrderedPostProcessors.add(pp);
                //判断类型是否是MergedBeanDefinitionPostProcessor,如果是则代码是内部使用的
                if (pp instanceof MergedBeanDefinitionPostProcessor) {
                    //将实例存入到容器internalPostProcessors
                    internalPostProcessors.add(pp);
                }
            } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
                //实现了Ordered接口的BeanPostProcessor
                //将name存入到容器orderedPostProcessorNames
                orderedPostProcessorNames.add(ppName);
            } else {
                 //没有实现排序接口的beanPostProcessor的name存入到容器nonOrderedPostProcessorNames
                nonOrderedPostProcessorNames.add(ppName);
            }
        }
		//排序实现了PriorityOrdered接口的BeanPostProcessor,也就是容器priorityOrderedPostProcessors中的实例
        sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
    	//N207   注册到BeanFactory中的beanPostProcessors这个List中
        registerBeanPostProcessors(beanFactory, (List)priorityOrderedPostProcessors);
    	//这个容器存实现了ordered接口的beanPostProcessor的实例
        List<BeanPostProcessor> orderedPostProcessors = new ArrayList();
        Iterator var14 = orderedPostProcessorNames.iterator();

        while(var14.hasNext()) {
            String ppName = (String)var14.next();
            //将上面存好name的容器遍历然后实例化bean并存入orderedPostProcessors容器
            BeanPostProcessor pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
            orderedPostProcessors.add(pp);
             //判断类型是否是MergedBeanDefinitionPostProcessor,如果是则代码是内部使用的
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }
        }
    	//排序
        sortPostProcessors(orderedPostProcessors, beanFactory);
    	//N207   注册到BeanFactory中的beanPostProcessors这个List中
        registerBeanPostProcessors(beanFactory, (List)orderedPostProcessors);
    	//这个容器存没有实现排序接口的beanPostProcessor的实例
        List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList();
        Iterator var17 = nonOrderedPostProcessorNames.iterator();

        while(var17.hasNext()) {
            ppName = (String)var17.next();
            //将上面存好name的容器遍历然后实例化bean并存入nonOrderedPostProcessors容器
            pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
            nonOrderedPostProcessors.add(pp);
            //判断类型是否是MergedBeanDefinitionPostProcessor,如果是则代码是内部使用的
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }
        }
		//N207   注册到BeanFactory中的beanPostProcessors这个List中
        registerBeanPostProcessors(beanFactory, (List)nonOrderedPostProcessors);
    	//最后排序内部使用的beanPostProcess的bean实例的容器
        sortPostProcessors(internalPostProcessors, beanFactory);
    	//N207   注册到BeanFactory中的beanPostProcessors这个List中
        registerBeanPostProcessors(beanFactory, (List)internalPostProcessors);
        beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
    }

PostProcessorRegistrationDelegate - 262 - N207
private static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {
        Iterator var2 = postProcessors.iterator();
		//N208   注册到BeanFactory中的beanPostProcessors这个List中
        while(var2.hasNext()) {
            BeanPostProcessor postProcessor = (BeanPostProcessor)var2.next();
            beanFactory.addBeanPostProcessor(postProcessor);
        }
    }
AbstractBeanFactory - 629 - N208
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
        Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
    	//移除旧的beanPostProcessor
        this.beanPostProcessors.remove(beanPostProcessor);
//接下来两个if判断,是为了标记容器中是否有InstantiationAwareBeanPostProcessor接口和DestructionAwareBeanPostProcessor接口这两种接口的实现类,根据有无分别记为true和false(初始化为false),而这个标记在后续会用到(bean的实例化过程中寻找带@autowired的构造器时,寻找@postconstruct注解时.....等等地方都用到了)
        if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
            //判断是否有实现InstantiationAwareBeanPostProcessor接口
            this.hasInstantiationAwareBeanPostProcessors = true;
        }
        if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
            //判断是否有实现DestructionAwareBeanPostProcessor接口
            this.hasDestructionAwareBeanPostProcessors = true;
        }
		//添加这个新的beanPostProcessor
        this.beanPostProcessors.add(beanPostProcessor);
    }
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值