Spring 5.x Source trip thirty-five getBean a processor extension points

Flowchart extension point processor

Here Insert Picture Description
First brought this figure, in the process of creating a basic bean that these, there may be other places I missed, to see the great God reminded ah, next time up. Then we come to the next one of these extensions do something.

Before instantiating resolveBeforeInstantiation

Eat before instantiation

	@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;
	}

Instantiated before applyBeanPostProcessorsBeforeInstantiation

InstantiationAwareBeanPostProcessorProcessor interface, such as an object can replace the other.

@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 initialization

The main front is returned if there is not empty, it will execute this to initialize.

	@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;
	}

Actual extension points

We just try extension point, I want to MyConfigclass replaced. Replaced and springwithout a class-dependent:
Here Insert Picture Description

MyInstantiationAwareBeanPostProcessor extension classes

postProcessBeforeInstantiationMethod returns MyBeforeInstantiationthe object, and then postProcessAfterInitializationinitialization properties 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 code

 @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);

In MyConfigprevious instances of replacement:
Here Insert Picture Description
After initialized age:
Here Insert Picture Description
Last singleton becomes this:
Here Insert Picture Description
Take out:
Here Insert Picture Description
Well, here today, and I hope to help study and understand, the great God saw do not spray, only their learning comprehension, ability limited him to bear.

Published 235 original articles · won praise 74 · views 30000 +

Guess you like

Origin blog.csdn.net/wangwei19871103/article/details/105166516