【spring原始碼】七、後置處理器BeanPostProcessor

hancoder發表於2020-12-20

registerBeanPostProcessors()

註冊(例項化)後置處理器

我們想要了解spring在什麼時候註冊自動代理,即@EnableAspectJAutoProxy等一系列執行的時機。這裡值得注意的是,@EnableAspectJAutoProxy的操作是在refresh()方法的invoke()階段完成了bean定義資訊的注入,而還沒完成例項化。例項化是在registerBeanPostProcessor註冊後置處理器階段完成的。

public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext, DisposableBean {
    
    protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
    }

我們呼叫registerBeanPostProcessors()的時候已經能從容器中拿到bean定義了,只是還是例項化。所以我們的流程是先例項化後置處理器,再例項化普通的bean,因為普通的bean要用到後置處理器。

呼叫棧:

  • refresh()
  • AbstractApplicationContext.registerBeanPostProcessors()
  • PostProcessorRegistrationDelegate.registerBeanPostProcessors()

流程:即拿到bean定義後,通過getBean生成bean例項,然後在beanFactory中有個beanPostProcessors屬是個list,我們把bean追加到這個list裡即可,但要注意順序,bean是有優先順序的。

getBean步驟:

  • 建立3個list用於儲存3種"要建立的後置處理器",把"beanPostProcessor定義資訊"按照priorityOrdered、ordered、nonOrdered分為3類,依次add到對應的list
  • 3個list按序呼叫(priorityOrdered最優先呼叫完)註冊這些後置處理器,註冊方式是通過beanFactory.getBean去獲取例項
  • 新建3個list用於儲存建立好的後置處理器。建立好後置處理器例項後依次add追加到對應的3個list,呼叫registerBeanPostProcessors(beanFactory, orderedPostProcessors);(另外一個)把後置處理器告訴工廠是什麼型別的。
PostProcessorRegistrationDelegate類;
//註冊bean的後置處理器來攔截bean的建立
public static void registerBeanPostProcessors(//前面是註冊定義,這裡是註冊
    ConfigurableListableBeanFactory beanFactory,//DefaultListableBeanFactory@3daa422a
    AbstractApplicationContext applicationContext)//AnnotationConfigApplicationContext@38bc8ab5
{

    // 拿到指定型別的bean(後置處理器)的名字 // postProcessorNames={internalAutowiredAnnotationProcessor、internalRequiredAnnotationProcessor、internalCommonAnnotationProcessor、internalAutoProxyCreater}4個元素。只是定義還沒建立物件
    String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);//4個
    //先獲取ioc容器已經定義了的需要建立物件的所有BeanPostProcessor的名字,這些物件是我們在配置類上註解的(比如我們之前的internalAutoProxyCreator),不是我們自己的
    
    // 3+1+4
    int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() //3 我們自己的
        + 1 
        + postProcessorNames.length;// 4個spring自帶的後置處理器

    // 給容器中加別的postProcessor //即上面的1
    beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
    
    List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
    List<BeanPostProcessor> internalPostProcessors = new ArrayList<BeanPostProcessor>();
    List<String> orderedPostProcessorNames = new ArrayList<String>();
    List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
    for (String ppName : postProcessorNames) {//4個
        if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {//優先註冊實現了PriorityOrdered介面的BeanPostProcessor
            // 包括3/4個:internalAutowiredAnnotationProcessor、internalRequiredAnnotationProcessor、internalCommonAnnotationProcessor
            BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);//只有PriorityOrdered型別的才在for裡註冊//beanFactory為DefaultListableBeanFactory
            priorityOrderedPostProcessors.add(pp);
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }
        }else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
            //包括1/4個:internalAutoProxyCreater
            orderedPostProcessorNames.add(ppName);//只是加在了名字的list中
        }
        else {
            nonOrderedPostProcessorNames.add(ppName);
        }
    }
    // 區分後依次註冊,加入到容器中
    // First, register the BeanPostProcessors that implement PriorityOrdered.
    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
    registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);//這個方法是我們之前說的new definition和註冊建立的函式

    // ---------------------------------
    // internalAutoProxyCreater在這裡getBean--------
    // Next, register the BeanPostProcessors that implement Ordered.//再註冊實現了Ordered介面的BeanPostProcessor//我們的@Enable就實現了這個介面
    List<BeanPostProcessor> orderedPostProcessors = new ArrayList<BeanPostProcessor>();
    for (String ppName : orderedPostProcessorNames) {// 1/4個
        // 例項化
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);//getBean//按名字從beanFactory中得到BeanPostProcessor//如果獲取不到就建立//註冊BeanPostProcessor實際上就是建立BeanPostProcessors物件,儲存在容器中。所以我們分析如何建立AnnotationAwareAspectJAutoProxyCreator類internalAutoProxyCreator名字的。
        orderedPostProcessors.add(pp);
        if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
        }
    }
    sortPostProcessors(orderedPostProcessors, beanFactory);
    registerBeanPostProcessors(beanFactory, orderedPostProcessors);

    // ---------------------------------
    // Now, register all regular BeanPostProcessors.//再註冊沒有實現優先順序介面的BeanPostProcessor
    List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
    for (String ppName : nonOrderedPostProcessorNames) {// 0/4個
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        nonOrderedPostProcessors.add(pp);
        if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
        }
    }
    registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

    // Finally, re-register all internal BeanPostProcessors.
    sortPostProcessors(internalPostProcessors, beanFactory);
    registerBeanPostProcessors(beanFactory, internalPostProcessors);

    // Re-register post-processor for detecting inner beans as ApplicationListeners,
    // moving it to the end of the processor chain (for picking up proxies etc).
    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
    
    //經測試,在這裡工廠中的bean定義已經是10個了,也就是說我們bean定義資訊一直在
    //((DefaultListableBeanFactory) beanFactory).alreadyCreated結果為5個,即5個internal:1個在上個階段建立的+4個本階段建立的
}

//-----------PostProcessorRegistrationDelegate-------------------------
private static void registerBeanPostProcessors(
    ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {
    //postProcessors依次為3個、1個、0個
    for (BeanPostProcessor postProcessor : postProcessors) {
        beanFactory.addBeanPostProcessor(postProcessor);
    }
}
//-----------AbstractBeanFactory-------------------------
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
    Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
    //this為beanFactory
    this.beanPostProcessors.remove(beanPostProcessor);//移除舊的
    this.beanPostProcessors.add(beanPostProcessor);//重新新增新的
    if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
        this.hasInstantiationAwareBeanPostProcessors = true;
    }
    if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
        this.hasDestructionAwareBeanPostProcessors = true;
    }
}

相關文章