/** * Instantiate and invoke all registered BeanFactoryPostProcessor beans, * respecting explicit order if given. * <p>Must be called before singleton instantiation. * <p> * 可以对调用的解析并注册的BeanDefinition进行修改或者新增 */ protectedvoidinvokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) { // 1.getBeanFactoryPostProcessors(): 拿到当前应用上下文beanFactoryPostProcessors变量中的值 // 2.invokeBeanFactoryPostProcessors: 实例化并调用所有已注册的BeanFactoryPostProcessor PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor) if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(newLoadTimeWeaverAwareProcessor(beanFactory)); beanFactory.setTempClassLoader(newContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } }
getBeanFactoryPostProcessor
1 2 3 4 5 6 7 8
/** * Return the list of BeanFactoryPostProcessors that will get applied * to the internal BeanFactory. * 获取所有注册的beanFactoryPostProcessors */ public List<BeanFactoryPostProcessor> getBeanFactoryPostProcessors() { returnthis.beanFactoryPostProcessors; }
// Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! // Separate between BeanDefinitionRegistryPostProcessors that implement // PriorityOrdered, Ordered, and the rest. // 用于保存本次要执行的BeanDefinitionRegistryPostProcessor List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = newArrayList<>();
// 3.调用所有实现PriorityOrdered接口的BeanDefinitionRegistryPostProcessor实现类 // 3.1 找出所有实现BeanDefinitionRegistryPostProcessor接口的Bean的beanName // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered. String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
// 5.最后, 调用所有剩下的BeanDefinitionRegistryPostProcessors //没实现排序接口的调用 // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear. booleanreiterate=true; while (reiterate) { //如果所以的BeanDefinitionRegistryPostProcessor都调用过了就退出循环 reiterate = false; //5.1 获取实现了BeanDefinitionRegistryPostProcessor接口的所有类的BeanDefinition对象的beanName postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); //遍历postProcessorNames for (String ppName : postProcessorNames) { // 5.2 跳过已经执行过的 //如果以上两次都没有调用过的BeanDefinitionRegistryPostProcessor走下面的判断 if (!processedBeans.contains(ppName)) { //因为需要进行调用所以先获取BeanDefinitionRegistryPostProcessor类型的实例并添加到currentRegistryProcessors currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); //将BeanDefinition对象的beanName添加到processedBeans processedBeans.add(ppName); //如果有调用的再次循环 // 5.3 如果有BeanDefinitionRegistryPostProcessor被执行, 则有可能会产生新的BeanDefinitionRegistryPostProcessor, // 因此这边将reiterate赋值为true, 代表需要再循环查找一次 reiterate = true; } } //完成排序工作 sortPostProcessors(currentRegistryProcessors, beanFactory); //添加到registryProcessors registryProcessors.addAll(currentRegistryProcessors); // 5.4 遍历currentRegistryProcessors, 执行postProcessBeanDefinitionRegistry方法 invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); //再次清空currentRegistryProcessors currentRegistryProcessors.clear(); }
//如果registryProcessors不为空的话最后一次完成一次调用 // 6.调用所有BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法(BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcessor) // Now, invoke the postProcessBeanFactory callback of all processors handled so far. invokeBeanFactoryPostProcessors(registryProcessors, beanFactory); // 7.最后, 调用入参beanFactoryPostProcessors中的普通BeanFactoryPostProcessor的postProcessBeanFactory方法 invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); } else { // Invoke factory processors registered with the context instance. //完成BeanFactoryPostProcessor的调用 invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory); }
// 到这里 , 入参beanFactoryPostProcessors和容器中的所有BeanDefinitionRegistryPostProcessor已经全部处理完毕, // 下面开始处理容器中的所有BeanFactoryPostProcessor // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! // 8.找出所有实现BeanFactoryPostProcessor接口的类 //获取实现了BeanFactoryPostProcessor接口的类,获取beanDefinition的名称 String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
// Separate between BeanFactoryPostProcessors that implement PriorityOrdered, // Ordered, and the rest. // 用于存放实现了PriorityOrdered接口的BeanFactoryPostProcessor List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = newArrayList<>(); // 用于存放实现了Ordered接口的BeanFactoryPostProcessor的beanName List<String> orderedPostProcessorNames = newArrayList<>(); // 用于存放普通BeanFactoryPostProcessor的beanName List<String> nonOrderedPostProcessorNames = newArrayList<>(); // 8.1 遍历postProcessorNames, 将BeanFactoryPostProcessor按实现PriorityOrdered、实现Ordered接口、普通三种区分开 for (String ppName : postProcessorNames) { // 8.2 跳过已经执行过的,如果上面步骤已经调用过的BeanDefinition跳过 if (processedBeans.contains(ppName)) { // skip - already processed in first phase above } //实现了PriorityOrdered接口的 elseif (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { // 8.3 添加实现了PriorityOrdered接口的BeanFactoryPostProcessor //实例化BeanFactoryPostProcessor并添加到priorityOrderedPostProcessors priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)); } //实现了Ordered接口的 elseif (beanFactory.isTypeMatch(ppName, Ordered.class)) { // 8.4 添加实现了Ordered接口的BeanFactoryPostProcessor的beanName orderedPostProcessorNames.add(ppName); } else { // 8.5 添加剩下的普通BeanFactoryPostProcessor的beanName nonOrderedPostProcessorNames.add(ppName); } }
// Clear cached merged bean definitions since the post-processors might have // modified the original metadata, e.g. replacing placeholders in values... // 12.清除元数据缓存(mergedBeanDefinitions、allBeanNamesByType、singletonBeanNamesByType), // 因为后处理器可能已经修改了原始元数据,例如, 替换值中的占位符... beanFactory.clearMetadataCache(); }
/** * 实现beanPostProcessor的注册 */ @Override publicvoidaddBeanPostProcessor(BeanPostProcessor beanPostProcessor) { Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null"); // Remove from old position, if any this.beanPostProcessors.remove(beanPostProcessor); // Track whether it is instantiation/destruction aware //是否支持实例化InstantiationAwareBeanPostProcessor if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) { this.hasInstantiationAwareBeanPostProcessors = true; } if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) { this.hasDestructionAwareBeanPostProcessors = true; } //将beanPostProcessor添加到beanPostProcessors // Add to end of list this.beanPostProcessors.add(beanPostProcessor); }