spring源码-BeanPostProcessor-3.3

  一、BeanPostProcessor这个是spring容器的拓展之一,是用于获取bean的时候处理对应的对象;

  二、常用场景,在获取bean的时候,重新初始化bean的属性等。

  三、实现方式(加入容器后,调用其他bean的时候,通过BeanPostProcessor来进行处理)

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class BeanPostProcessorTest implements BeanPostProcessor{

    /**
     * 在bean加载之前处理
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("before");
        return bean;
    }

    /**
     * 在bean加载之后处理
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("after");
        return bean;
    }
}

  四、源码部分

  1)容器中的注册部分为refresh()中的registerBeanPostProcessors()方法,注意这里只是注册,实际调用在getBean的时候

this.registerBeanPostProcessors(beanFactory);

  2)registerBeanPostProcessors

protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        //在容器中获取获取实现BeanPostProcessor的bean
        String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
        int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
        beanFactory.addBeanPostProcessor(new AbstractApplicationContext.BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
        //声明不同BeanPostProcessor类型(priorityOrdered,internal,ordered,non 按照顺序,和优先级执行)
        List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList();
        List<BeanPostProcessor> internalPostProcessors = new ArrayList();
        List<String> orderedPostProcessorNames = new ArrayList();
        List<String> nonOrderedPostProcessorNames = new ArrayList();
        String[] var11 = postProcessorNames;
        int var10 = postProcessorNames.length;

        BeanPostProcessor pp;
        for(int var9 = 0; var9 < var10; ++var9) {
            String ppName = var11[var9];
            //判断时什么类型
            if (this.isTypeMatch(ppName, PriorityOrdered.class)) {
                pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
                priorityOrderedPostProcessors.add(pp);
                //此接口存在postProcessMergedBeanDefinition的接口
                if (pp instanceof MergedBeanDefinitionPostProcessor) {
                    internalPostProcessors.add(pp);
                }
            } else if (this.isTypeMatch(ppName, Ordered.class)) {
                orderedPostProcessorNames.add(ppName);
            } else {
                nonOrderedPostProcessorNames.add(ppName);
            }
        }
        //排序
        OrderComparator.sort(priorityOrderedPostProcessors);
        //添加到beanPostProcessors(private final List<BeanPostProcessor> beanPostProcessors = new ArrayList();)
        this.registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
        List<BeanPostProcessor> orderedPostProcessors = new ArrayList();
        Iterator var16 = orderedPostProcessorNames.iterator();

        while(var16.hasNext()) {
            String ppName = (String)var16.next();
            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 var19 = nonOrderedPostProcessorNames.iterator();

        while(var19.hasNext()) {
            String ppName = (String)var19.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);
        this.registerBeanPostProcessors(beanFactory, internalPostProcessors);
        beanFactory.addBeanPostProcessor(new AbstractApplicationContext.ApplicationListenerDetector((AbstractApplicationContext.ApplicationListenerDetector)null));
    }

    private void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {
        Iterator var4 = postProcessors.iterator();

        while(var4.hasNext()) {
            BeanPostProcessor postProcessor = (BeanPostProcessor)var4.next();
            //添加到beanPostProcessors(private final List<BeanPostProcessor> beanPostProcessors = new ArrayList();)
            beanFactory.addBeanPostProcessor(postProcessor);
        }
    }

  3)addBeanPostProcessor(DefaultListableBeanFactory,中缓存的List<String> beanDefinitionNames = new ArrayList())

  public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
        Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
        this.beanPostProcessors.remove(beanPostProcessor);
        //添加到beanPostProcessors中
        this.beanPostProcessors.add(beanPostProcessor);
        if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
            this.hasInstantiationAwareBeanPostProcessors = true;
        }

        if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
            this.hasDestructionAwareBeanPostProcessors = true;
        }
    }

  五、调用方式(在个getBean的时候回自动按照优先级调用BeanPostProcessor)

  具体实现见:spring源码-bean之加载-2的四、源码实现3).d createBean部分

 

posted @ 2018-11-15 13:45  小不点丶  阅读(375)  评论(0编辑  收藏  举报