2023-09-16
原文作者:王伟王胖胖 原文地址: https://blog.csdn.net/wangwei19871103/article/details/105166516

处理器扩展点流程图

202309162313231011.png
先把这个图送上,在创建bean的过程中基本就是这些了,可能还有其他地方我漏了,看到的大神提醒下啊,下次补。然后我们一个个来将下这些扩展点能做什么。

resolveBeforeInstantiation实例化之前

在实例化之前吃

    	@Nullable
    	protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
    		Object bean = null;
    		if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
    			// Make sure bean class is actually resolved at this point.
    			if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {//非合成的,且有InstantiationAwareBeanPostProcessors
    				Class<?> targetType = determineTargetType(beanName, mbd);//获取类型
    				if (targetType != null) {
    					bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
    					if (bean != null) {
    						bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
    					}
    				}
    			}
    			mbd.beforeInstantiationResolved = (bean != null);//是否解析了
    		}
    		return bean;
    	}

applyBeanPostProcessorsBeforeInstantiation实例化之前

InstantiationAwareBeanPostProcessor接口的处理器处理,比如可以把一个对象替换成另外一个。

    @Nullable
    	protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
    		for (BeanPostProcessor bp : getBeanPostProcessors()) {
    			if (bp instanceof InstantiationAwareBeanPostProcessor) {
    				InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
    				Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
    				if (result != null) {
    					return result;
    				}
    			}
    		}
    		return null;
    	}

applyBeanPostProcessorsAfterInitialization初始化

这个主要是前面如果有返回不为空的,就会执行这个来初始化。

    	@Override
    	public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
    			throws BeansException {
    
    		Object result = existingBean;
    		for (BeanPostProcessor processor : getBeanPostProcessors()) {
    			Object current = processor.postProcessAfterInitialization(result, beanName);
    			if (current == null) {
    				return result;
    			}
    			result = current;
    		}
    		return result;
    	}

扩展点实战

我们来试试刚才的扩展点,我想把MyConfig类替换掉。替换成和spring无依赖的一个类:

202309162313238082.png

MyInstantiationAwareBeanPostProcessor扩展类

postProcessBeforeInstantiation方法返回MyBeforeInstantiation对象,然后postProcessAfterInitialization初始化属性age

    @Component
    public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor
    {
    
        @Override
        public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
            return new MyBeforeInstantiation();
        }
    
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            MyBeforeInstantiation myBeforeInstantiation=(MyBeforeInstantiation)bean;
            myBeforeInstantiation.age=10;
            return null;
        }
    }

测试代码

     @Test
        public void BeanDefinitionRegistryPostProcessorTest0() throws Exception {
            AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
            applicationContext.register(MyConfig.class);
            applicationContext.refresh();
            MyBeforeInstantiation myBeforeInstantiation = applicationContext.getBean(MyBeforeInstantiation.class);
            System.out.println(myBeforeInstantiation.age);

MyConfig实例化之前替换:

202309162313242493.png
之后就初始化age

202309162313246514.png
最后单例就变这样:

202309162313251695.png
取出来了:

202309162313255666.png
好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。

阅读全文