1、源码解析
registerBeanPostProcessors(beanFactory); 注册bean的后置处理器。1 // 按照指定的顺序实例化并注册bean的后置处理器,仅做注册 2 protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) { 3 PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this); 4 }
1.1、PostProcessorRegistrationDelegate简介
PostProcessorRegistrationDelegate是构造器私有、AbstractApplicationContext容器的后置处理器委托类。![](/i/l/?n=23&i=blog/1680081/202212/1680081-20221224192706252-727975441.png)
PostProcessorRegistrationDelegate主要用在容器初始化时 - BeanFactory的后置处理器执行、Bean的后置处理器注册环节。
该委托类的主要作用: 1. 调用BeanFactory的后置处理器 - invokeBeanFactoryPostProcessors; 2. 注册Bean的后置处理器 - registerBeanPostProcessors。1.2、注册BeanPostProcessor
registerBeanPostProcessors(beanFactory)是PostProcessorRegistrationDelegate的静态方法。1 // 注册Bean的后置处理器 2 public static void registerBeanPostProcessors( 3 ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) { 4 5 // 找到所有实现了BeanPostProcessor接口的类名称 6 String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false); 7 8 // 记录下BeanPostProcessor的目标计数 9 // 此处+1,是因为在此方法的最后会添加一个BeanPostProcessorChecker的类 10 int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length; 11 // 添加BeanPostProcessorChecker(主要用于记录信息)到beanFactory中 12 beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount)); 13 14 // 定义存放实现了PriorityOrdered接口的BeanPostProcessor集合 15 List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>(); 16 // 定义存放spring内部的BeanPostProcessor 17 List<BeanPostProcessor> internalPostProcessors = new ArrayList<>(); 18 // 定义存放实现了Ordered接口的BeanPostProcessor的name集合 19 List<String> orderedPostProcessorNames = new ArrayList<>(); 20 // 定义存放普通的BeanPostProcessor的name集合 21 List<String> nonOrderedPostProcessorNames = new ArrayList<>(); 22 // 遍历beanFactory中存在的BeanPostProcessor的集合postProcessorNames 23 for (String ppName : postProcessorNames) { 24 // 如果ppName对应的BeanPostProcessor实例实现了PriorityOrdered接口, 25 // 则获取到ppName对应的BeanPostProcessor的实例添加到priorityOrderedPostProcessors中 26 if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { 27 // 通过类名、所属类型获取BeanPostProcessor的实例对象 28 BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); 29 priorityOrderedPostProcessors.add(pp); 30 // 如果ppName对应的BeanPostProcessor实例也实现了MergedBeanDefinitionPostProcessor接口, 31 // 那么则将ppName对应的bean实例添加到internalPostProcessors中 32 if (pp instanceof MergedBeanDefinitionPostProcessor) { 33 internalPostProcessors.add(pp); 34 } 35 } 36 // 如果ppName对应的BeanPostProcessor实例没有实现PriorityOrdered接口, 37 // 但是实现了Ordered接口,那么将ppName对应的bean实例添加到orderedPostProcessorNames中 38 else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { 39 orderedPostProcessorNames.add(ppName); 40 } 41 // 否则将ppName添加到nonOrderedPostProcessorNames中 42 else { 43 nonOrderedPostProcessorNames.add(ppName); 44 } 45 } 46 47 // 首先,对实现了PriorityOrdered接口的BeanPostProcessor实例进行排序操作 48 sortPostProcessors(priorityOrderedPostProcessors, beanFactory); 49 // 注册实现了PriorityOrdered接口的BeanPostProcessor实例添加到beanFactory中 50 registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors); 51 52 // Next, register the BeanPostProcessors that implement Ordered. 53 // 注册所有实现Ordered的beanPostProcessor 54 List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size()); 55 for (String ppName : orderedPostProcessorNames) { 56 // 根据ppName找到对应的BeanPostProcessor实例对象 57 BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); 58 // 将实现了Ordered接口的BeanPostProcessor添加到orderedPostProcessors集合中 59 orderedPostProcessors.add(pp); 60 // 如果ppName对应的BeanPostProcessor实例也实现了MergedBeanDefinitionPostProcessor接口,那么则将ppName对应的bean实例添加到internalPostProcessors中 61 if (pp instanceof MergedBeanDefinitionPostProcessor) { 62 internalPostProcessors.add(pp); 63 } 64 } 65 // 对实现了Ordered接口的BeanPostProcessor进行排序操作 66 sortPostProcessors(orderedPostProcessors, beanFactory); 67 // 注册实现了Ordered接口的BeanPostProcessor实例添加到beanFactory中 68 registerBeanPostProcessors(beanFactory, orderedPostProcessors); 69 70 // Now, register all regular BeanPostProcessors. 71 // 创建存放没有实现PriorityOrdered和Ordered接口的BeanPostProcessor的集合 72 List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size()); 73 // 遍历集合 74 for (String ppName : nonOrderedPostProcessorNames) { 75 // 根据ppName找到对应的BeanPostProcessor实例对象 76 BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); 77 // 将没有实现PriorityOrdered和Ordered接口的BeanPostProcessor添加到nonOrderedPostProcessors集合中 78 nonOrderedPostProcessors.add(pp); 79 // 如果ppName对应的BeanPostProcessor实例也实现了MergedBeanDefinitionPostProcessor接口,那么则将ppName对应的bean实例添加到internalPostProcessors中 80 if (pp instanceof MergedBeanDefinitionPostProcessor) { 81 internalPostProcessors.add(pp); 82 } 83 } 84 // 注册没有实现PriorityOrdered和Ordered的BeanPostProcessor实例添加到beanFactory中 85 registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors); 86 87 // Finally, re-register all internal BeanPostProcessors. 88 // 将所有实现了MergedBeanDefinitionPostProcessor类型的BeanPostProcessor进行排序操作 89 sortPostProcessors(internalPostProcessors, beanFactory); 90 // 注册所有实现了MergedBeanDefinitionPostProcessor类型的BeanPostProcessor到beanFactory中 91 registerBeanPostProcessors(beanFactory, internalPostProcessors); 92 93 // Re-register post-processor for detecting inner beans as ApplicationListeners, 94 // moving it to the end of the processor chain (for picking up proxies etc). 95 // 注册ApplicationListenerDetector到beanFactory中 96 beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext)); 97 }
1、获取所有的实现了BeanPostProcessor接口的类名称数组;
2、遍历类名称数组并初始化实现不通接口的集合 ·实现了PriorityOrdered接口的BeanPostProcessor,通过getBean获取对应类名称的实例对象放入priorityOrderedPostProcessors 集合中; ·实现了PriorityOrdered接口并且实现了MergedBeanDefinitionPostProcessor接口的BeanPostProcessor,通过getBean获取对应类名称的实例对象放入internalPostProcessors集合中; ·实现了Ordered接口的BeanPostProcessor放入orderedPostProcessorNames集合中; ·未实现上面接口的BeanPostProcessor放入nonOrderedPostProcessorNames集合中。 3、对priorityOrderedPostProcessors 集合做排序,并将给定集合中的BeanPostProcessor对象注册到容器中。 4、遍历实现了Ordered接口的BeanPostProcessor类名称的orderedPostProcessorNames集合,通过getBean获取类名称对应的BeanPostProcessor实例对象, 将实现了Ordered接口实例对象放入orderedPostProcessors集合中;若该实例对象实现了MergedBeanDefinitionPostProcessor接口,则将对象放入internalPostProcessors集合中。对orderedPostProcessors集合做排序,并将给定集合中的BeanPostProcessor对象注册到容器中。 5、将没有实现PriorityOrdered和Ordered接口的BeanPostProcessor放入nonOrderedPostProcessors集合中,若该实例对象实现了MergedBeanDefinitionPostProcessor接口,则将对象放入internalPostProcessors集合中,将给定集合中的BeanPostProcessor对象注册到容器中。 6、对实现了MergedBeanDefinitionPostProcessor接口的BeanPostProcessor对象集合internalPostProcessors做排序,将给定集合中的BeanPostProcessor对象注册到容器中。 上述的所有BeanPostProcessor都需要注册到容器中,下面我们来看看是如何注册到容器中的。1 // 将给定的 BeanPostprocessor 注册到容器中 2 private static void registerBeanPostProcessors( 3 ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) { 4 for (BeanPostProcessor postProcessor : postProcessors) { 5 beanFactory.addBeanPostProcessor(postProcessor); 6 } 7 }
注册Bean后置处理器的核心方法:addBeanPostProcessor(beanPostProcessor),在ConfigurableBeanFactory接口中定义,由其子类AbstractBeanFactory(抽象类)实现。
1 // Bean销毁置处理器是否已经被注册标识 2 private volatile boolean hasDestructionAwareBeanPostProcessors; 3 // 实例化后置处理器是否已经被注册标识 4 private volatile boolean hasInstantiationAwareBeanPostProcessors; 5 // 容器中后置处理器集合 6 private final List<BeanPostProcessor> beanPostProcessors = new CopyOnWriteArrayList<>(); 7 // 将BeanPostProcessor加入容器 8 public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) { 9 // BeanPostProcessor对象非空校验 10 Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null"); 11 // 如果beanPostProcessors集合中已经存在此beanPostProcessors对象,移除该对象 12 this.beanPostProcessors.remove(beanPostProcessor); 13 // beanPostProcessor为InstantiationAwareBeanPostProcessor类型,Bean对象实例化后置处理器注册标识设置为true 14 if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) { 15 this.hasInstantiationAwareBeanPostProcessors = true; 16 } 17 // beanPostProcessor为DestructionAwareBeanPostProcessor类型,Bean对象销毁后置处理器注册标识设置为true 18 if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) { 19 this.hasDestructionAwareBeanPostProcessors = true; 20 } 21 // 将beanPostProcessor加入后置处理器集合中 22 this.beanPostProcessors.add(beanPostProcessor); 23 }
1、容器中若已存在此BeanPostProcessor,删除容器中的BeanPostProcessor;
2、设置Bean实例化后置处理器是否已注册标识、Bean对象销毁后置处理器是否已注册标识; 3、将方法入参中的BeanPostProcessor注册到容器中的beanPostProcessors集合中。