序言
之前我们用大量的篇幅介绍过invokeBeanFactoryPostProcessors()
方法的执行流程。
而invokeBeanFactoryPostProcessors
的主要逻辑就是遍历执行实现了BeanDefinitionRegistryPostProcesso类(主要是针对BeanDefinition的操作)和BeanFactoryPostProcessor(主要针对BeanFacroty的操作)。
我们这篇文章里要介绍的registerBeanPostProcessors()
方法 和 invokeBeanFactoryPostProcessors()
方法类似,作用对象是Bean,用于在 Spring 容器实例化、配置和初始化 bean 的过程中提供自定义的扩展点。
源码
获取系统中实现BeanPostProcessor
的类并进行分类,添加到BeanFacroty中。处理逻辑和BeanPosrProcessor基本相似。
public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {//获取所有实现了BeanPostProcessor类的BeanNameString[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);// Register BeanPostProcessorChecker that logs an info message when// a bean is created during BeanPostProcessor instantiation, i.e. when// a bean is not eligible for getting processed by all BeanPostProcessors.//这里的 +1,应该是为了为下方的 new BeanPostProcessorChecker 留个位置//创建的BeanPostProcessorChecker是用来int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));// Separate between BeanPostProcessors that implement PriorityOrdered,// Ordered, and the rest.//用来存放实现riorityOrdered的BeanPostProcessorList<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();//用来存放实现MergedBeanDefinitionPostProcessor的BeanPostProcessorList<BeanPostProcessor> internalPostProcessors = new ArrayList<>();//用来存放实现Ordered的BeanPostProcessorList<String> orderedPostProcessorNames = new ArrayList<>();//用来存放没有实现排序接口的BeanPostProcessorList<String> nonOrderedPostProcessorNames = new ArrayList<>();//遍历获取所有的BeanPostProcessorfor (String ppName : postProcessorNames) {//判断是否实现了PriorityOrdered接口if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {//获取BeanPostProcessorBeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);priorityOrderedPostProcessors.add(pp);//判断是否实现了MergedBeanDefinitionPostProcessor接口if (pp instanceof MergedBeanDefinitionPostProcessor) {internalPostProcessors.add(pp);}}//如果实现了Ordered接口,则添加到orderedPostProcessorNames中else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {orderedPostProcessorNames.add(ppName);}else {//否则就是没有实现排序接口的类nonOrderedPostProcessorNames.add(ppName);}}// First, register the BeanPostProcessors that implement PriorityOrdered.//根据优先级进行排序sortPostProcessors(priorityOrderedPostProcessors, beanFactory);//注册(循环添加到BeanFactory的beanPostProcessors集合中)实现priorityOrder接口的BeanPostProcessorregisterBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);// Next, register the BeanPostProcessors that implement Ordered.//注册(循环添加到BeanFactory的beanPostProcessors集合中)实现Ordered接口的BeanPostProcessorList<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());for (String ppName : orderedPostProcessorNames) {BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);orderedPostProcessors.add(pp);if (pp instanceof MergedBeanDefinitionPostProcessor) {internalPostProcessors.add(pp);}}sortPostProcessors(orderedPostProcessors, beanFactory);registerBeanPostProcessors(beanFactory, orderedPostProcessors);// Now, register all regular BeanPostProcessors.//最后注册(循环添加到BeanFactory的beanPostProcessors集合中)没有实现排序接口的BeanPostProcessorList<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());for (String ppName : nonOrderedPostProcessorNames) {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.//重新注册(循环添加到BeanFactory的beanPostProcessors集合中)实现MergedBeanDefinitionPostProcessor接口的BeanPostProcessorsortPostProcessors(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).//注册ApplicationListenerDetector,// 其实refresh()主流程方法下的prepareBeanFactory(beanFactory)方法中已经向beanFactory中添加了ApplicationListenerDetector//这里是重新注册,保证ApplicationListenerDetector在beanPostProcessors集合的最后//目的是检测并管理应用程序上下文中的事件监听器。beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
扩展
值得说的地方是Spirng提供了几个比较重要的BeanPostProcessor
接口可以用来进行扩展。因为其与四个接口都继承自BeanPostProcessor
所以BeanPostProcessor
中的方法他们也都有。
挨个接口来看看里面都有什么。
BeanPostProcessor
bean的后置处理器接口,在依赖注入的初始化方法前后进行调用。
/*** bean的后置处理器接口,在依赖注入的初始化方法前后进行调用*/
public interface BeanPostProcessor {/*** 初始化方法调用前要进行的处理逻辑*/@Nullabledefault Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}/*** 在初始化方法指定后要进行的处理逻辑*/@Nullabledefault Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}}
InstantiationAwareBeanPostProcessor
新增了属性注入的方法。
/*** 继承自BeanPostProcessor,添加了实例化前,实例化后,属性注入后的处理方法*/
public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {//省略BeanPostProcessor方法.../*** 当使用注解的时候,通过这个方法来完成属性的注入*/@Nullabledefault PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)throws BeansException {return null;}/*** 属性注入后执行的方法,在5.1版本被废弃*/@Deprecated@Nullabledefault PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {return pvs;}}
SmartInstantiationAwareBeanPostProcessor
继承自InstantiationAwareBeanPostProcessor
,额外增加3个方法。
/*** 继承自InstantiationAwareBeanPostProcessor接口,增加了三个额外处理的方法,由spring内部使用**/
public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor {/*** 预测bean的类型,主要是在bean还没有创建前我们需要获取bean的类型**/@Nullabledefault Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {return null;}/*** 完成对构造函数的解析和推断*/@Nullabledefault Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName)throws BeansException {return null;}/*** 解决循环依赖问题,通过此方法提前暴露一个合格的对象**/default Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {return bean;}}
MergedBeanDefinitionPostProcessor
两个BeanDefinition合并时调用。
public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {/***spring通过此方法找出所有需要注入的字段,同时做缓存*/void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);/*** 用于在BeanDefinition被修改后,清除容器的缓存*/default void resetBeanDefinition(String beanName) {}
}
DestructionAwareBeanPostProcessor
判断Bean是否应该销毁和销毁时调用
public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {/*** 在bean被销毁前调用*/void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;/*** 判断是否要进行销毁,一般情况下都需要*/default boolean requiresDestruction(Object bean) {return true;}
}