BeanPostProcessor原理學習

《Spring源碼解析》筆記app

一、自定義的BeanPostProcessoride

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
        return bean;
    }
}

二、其中postProcessBeforeInitialization函數的調用鏈以下:函數

   在容器啓動的時候:refresh()-->finishBeanFactoryInitialization(beanFactory)-->beanFactory.preInstantiateSingletons()->this.getBean(beanName)-->post

                                  this.doGetBean(name, (Class)null, (Object[])null, false)--this.getSingleton(beanName, new ObjectFactory<Object>()-->this

                                  singletonFactory.getObject()-->AbstractBeanFactory.this.createBean(beanName, mbd, args)-->this.doCreateBean(beanName, mbdToUse, args)spa

                                 -->this.initializeBean(beanName, exposedObject, mbd) code

                             

     在initializeBean()執行以前,首先執行的是populateBean()函數,該函數主要是爲Bean的各個屬性賦值。blog

     this.populateBean(beanName, mbd, instanceWrapper);
        if (exposedObject != null) {
             exposedObject = this.initializeBean(beanName, exposedObject, mbd);
        }

三、在initializeBean(beanName, exposedObject, mbd) 中觀察調用的代碼接口

    if (mbd == null || !mbd.isSynthetic()) {
            wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
        }
    //初始化方法執行代碼
        try {
            this.invokeInitMethods(beanName, wrappedBean, mbd);
        } catch (Throwable var6) {
            throw new BeanCreationException(mbd != null ? mbd.getResourceDescription() : null, beanName, "Invocation of init method failed", var6);
        }

        if (mbd == null || !mbd.isSynthetic()) {
            wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
        }

四、查看applyBeanPostProcessorsBeforeInitialization和applyBeanPostProcessorsAfterInitialization函數代碼ip

    遍歷獲得容器中全部的BeanPostProcessor;挨個執行beforeInitialization,一但返回null,跳出for循環,不會執行後面的BeanPostProcessor.postProcessorsBeforeInitialization

   對於容器中每個Bean實例,只要符合步驟3中if語句條件,都會執行這兩個函數,並非實現BeanPostProcessor接口才會進入這兩個函數。

@Override
    public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
            throws BeansException {

        Object result = existingBean;
        for (BeanPostProcessor processor : getBeanPostProcessors()) {
            result = processor.postProcessBeforeInitialization(result, beanName);
            if (result == null) {
                return result;
            }
        }
        return result;
    }

    @Override
    public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
            throws BeansException {

        Object result = existingBean;
        for (BeanPostProcessor processor : getBeanPostProcessors()) {
            result = processor.postProcessAfterInitialization(result, beanName);
            if (result == null) {
                return result;
            }
        }
        return result;
    }
相關文章
相關標籤/搜索