registerBeanPostProcessors浅析

执行registerBeanPostProcessors

  • 获取了实现了BeanPostProcessors接口的实现类
beanName bean
org.springframework.context.annotation.internalAutowiredAnnotationProcessor AutowiredAnnotationBeanPostProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor CommonAnnotationBeanPostProcessor
  • 把BeanPostProcessorChecker对象注册(添加)到beanFactory#beanPostProcessors中。
  • 对实现了PriorityOrdered接口的BeanPostProcessors实现类优先进行bean的创建和初始化,然后把初始化好的bean添加到priorityOrderedPostProcessors List接中,如果当前的bean同时也实现了MergedBeanDefinitionPostProcessor接口那么把它添加到 internalPostProcessors List集合中;对实现了Ordered接口的BeanPostProcessors实现类,把beanName添加到orderedPostProcessorNames List集合中;对未实现PriorityOrdered和Ordered接口的BeanPostProcessors实现类对应的beanName添加到nonOrderedPostProcessorNames List集合中。
  • priorityOrderedPostProcessors集合进行排序。
  • priorityOrderedPostProcessors集合中的数据注册(添加)到beanFactory#beanPostProcessors中。
  • 遍历orderedPostProcessorNames 对bean进行创建及初始化,然后添加到orderedPostProcessors List集合中,把同时也实现了MergedBeanDefinitionPostProcessor接口的bean 添加到 internalPostProcessors List集合中。
  • orderedPostProcessors 集合进行排序。
  • orderedPostProcessors集合中的数据注册(添加)到beanFactory#beanPostProcessors中。
  • 遍历nonOrderedPostProcessorNames 对bean进行创建及初始化,然后添加到nonOrderedPostProcessors List集合中,把同时也实现了MergedBeanDefinitionPostProcessor接口的bean 添加到 internalPostProcessors List集合中。
  • nonOrderedPostProcessors集合中的数据注册(添加)到beanFactory#beanPostProcessors中。
  • internalPostProcessors List集合进行排序。
  • internalPostProcessors集合中的数据注册(添加)到beanFactory#beanPostProcessors中。
  • ApplicationListenerDetector 对象注册(添加)到beanFactory#beanPostProcessors中。

beanFactory#beanPostProcessors注册(添加)操作,都是先移除掉已经存在的对象,然后在注册(添加)

beanPostProcessors

beanPostProcessors对应的实际对象为BeanPostProcessorCacheAwareList,当调用add和remove方法时会发现都对beanPostProcessorCache 置null 处理。

@Override
public boolean add(BeanPostProcessor o) {
	boolean success = super.add(o);
	beanPostProcessorCache = null;
	return success;
}
@Override
public BeanPostProcessor remove(int index) {
	BeanPostProcessor result = super.remove(index);
	beanPostProcessorCache = null;
	return result;
}

这是为了在下一次调用getBeanPostProcessorCache方法时再次对beanPostProcessorCache进行创建,通过把beanPostProcessors对应的bean进行遍历,对不同类型的bean进行分类对beanPostProcessorCache的数据进行初始化。

初始化数据 实现的接口
instantiationAware InstantiationAwareBeanPostProcessor
smartInstantiationAware SmartInstantiationAwareBeanPostProcessor
destructionAware DestructionAwareBeanPostProcessor
mergedDefinition MergedBeanDefinitionPostProcessor
BeanPostProcessorCache getBeanPostProcessorCache() {
	BeanPostProcessorCache bpCache = this.beanPostProcessorCache;
	if (bpCache == null) {
		bpCache = new BeanPostProcessorCache();
		for (BeanPostProcessor bp : this.beanPostProcessors) {
			if (bp instanceof InstantiationAwareBeanPostProcessor) {
				bpCache.instantiationAware.add((InstantiationAwareBeanPostProcessor) bp);
				if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
					bpCache.smartInstantiationAware.add((SmartInstantiationAwareBeanPostProcessor) bp);
				}
			}
			if (bp instanceof DestructionAwareBeanPostProcessor) {
				bpCache.destructionAware.add((DestructionAwareBeanPostProcessor) bp);
			}
			if (bp instanceof MergedBeanDefinitionPostProcessor) {
				bpCache.mergedDefinition.add((MergedBeanDefinitionPostProcessor) bp);
			}
		}
		this.beanPostProcessorCache = bpCache;
	}
	return bpCache;
}

部分作用

比如在执行createBean方法逻辑时会调用resolveBeforeInstantiation方法,判断instantiationAware数据不为空,则循环调用InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation获取对象,如果获取的对象不为空,则循环掉用beanFactory#beanPostProcessors的postProcessAfterInitialization方法最终获取一个对象。最后把这个对象赋值给BeanDefinition的beforeInstantiationResolved属性,实际上这部分逻辑是让BeanPostProcessors返回一个代理,而不是目标bean实例,也就是说如果通过resolveBeforeInstantiation方法如果返回了对象则不会再通过反射来创建bean的实例。

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()) {
			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;
}

你可能感兴趣的:(Spring,Boot,Spring,java,spring,前端)