protectedvoidinvokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory){//调用BeanFactory后置处理器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()));}}
publicstaticvoidinvokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory,List<BeanFactoryPostProcessor> beanFactoryPostProcessors){// Invoke BeanDefinitionRegistryPostProcessors first, if any.Set<String> processedBeans =newHashSet<>();//是否实现BeanDefinitionRegistryif(beanFactory instanceofBeanDefinitionRegistry){BeanDefinitionRegistry registry =(BeanDefinitionRegistry) beanFactory;List<BeanFactoryPostProcessor> regularPostProcessors =newArrayList<>();//BeanDefinitionRegistryPostProcessor集合List<BeanDefinitionRegistryPostProcessor> registryProcessors =newArrayList<>();//遍历所有的BeanFactoryPostProcessorfor(BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors){if(postProcessor instanceofBeanDefinitionRegistryPostProcessor){BeanDefinitionRegistryPostProcessor registryProcessor =(BeanDefinitionRegistryPostProcessor) postProcessor;//调用 BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法 处理bean的定义
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);}else{
regularPostProcessors.add(postProcessor);}}// 当前BeanDefinitionRegistryPostProcessor接口集合List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors =newArrayList<>();// 1, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.//第一步获取所有实现PriorityOrdered接口的BeanDefinitionRegistryPostProcessorString[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class,true,false);for(String ppName : postProcessorNames){if(beanFactory.isTypeMatch(ppName,PriorityOrdered.class)){
currentRegistryProcessors.add(beanFactory.getBean(ppName,BeanDefinitionRegistryPostProcessor.class));//完成实例化加入容器
processedBeans.add(ppName);}}//排序sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);//调用 BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.//2、调用实现了Ordered排序接口的BeanDefinitionRegistryPostProcessor
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class,true,false);for(String ppName : postProcessorNames){//判断是否是实现的Ordered接口if(!processedBeans.contains(ppName)&& beanFactory.isTypeMatch(ppName,Ordered.class)){//触发对应类的实例化getBean
currentRegistryProcessors.add(beanFactory.getBean(ppName,BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);}}sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.//3、没有实现接口的BeanDefinitionRegistryPostProcessor调用boolean reiterate =true;while(reiterate){
reiterate =false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class,true,false);for(String ppName : postProcessorNames){if(!processedBeans.contains(ppName)){
currentRegistryProcessors.add(beanFactory.getBean(ppName,BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
reiterate =true;}}sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();}//调用postProcessBeanFactory方法// Now, invoke the postProcessBeanFactory callback of all processors handled so far.invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);}else{//非实现BeanDefinitionRegistry接口// Invoke factory processors registered with the context instance.invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);}//2.BeanFactoryPostProcessor方法调用String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class,true,false);//实现了PriorityOrdered排序接口的BeanFactoryPostProcessorList<BeanFactoryPostProcessor> priorityOrderedPostProcessors =newArrayList<>();//实现了ordered接口的List<String> orderedPostProcessorNames =newArrayList<>();List<String> nonOrderedPostProcessorNames =newArrayList<>();for(String ppName : postProcessorNames){//processedBeans已经调用过的if(processedBeans.contains(ppName)){}//实现了PriorityOrdered排序接口的BeanFactoryPostProcessorelseif(beanFactory.isTypeMatch(ppName,PriorityOrdered.class)){//触发getBean还没有invoke下面invoke
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName,BeanFactoryPostProcessor.class));}//2.2、实现了Ordered接口的(只是把beanName添加到name集合下面遍历name集合getBean)elseif(beanFactory.isTypeMatch(ppName,Ordered.class)){
orderedPostProcessorNames.add(ppName);}else{//2.3、没实现接口的(只是把beanName添加到name集合下面遍历name集合getBean)
nonOrderedPostProcessorNames.add(ppName);}}//排序sortPostProcessors(priorityOrderedPostProcessors, beanFactory);//调用实现了PriorityOrdered排序接口的BeanFactoryPostProcessor的postProcessBeanFactory方法invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);// getBean实现了ordered接口的BeanFactoryPostProcessorList<BeanFactoryPostProcessor> orderedPostProcessors =newArrayList<>(orderedPostProcessorNames.size());for(String postProcessorName : orderedPostProcessorNames){
orderedPostProcessors.add(beanFactory.getBean(postProcessorName,BeanFactoryPostProcessor.class));}sortPostProcessors(orderedPostProcessors, beanFactory);//调用invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);// 其他List<BeanFactoryPostProcessor> nonOrderedPostProcessors =newArrayList<>(nonOrderedPostProcessorNames.size());for(String postProcessorName : nonOrderedPostProcessorNames){
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName,BeanFactoryPostProcessor.class));}invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);//总结: 对上面接口的理解:获取BeanDefinitionRegistry对象,获取到这个对象就可以获取这个对象中注册的所有BeanDefinition对象,我们拥有这个对象就可以完成里面所有BeanDefinition对象的修改我新增操作// Clear cached merged bean definitions since the post-processors might have// modified the original metadata, e.g. replacing placeholders in values...
beanFactory.clearMetadataCache();}
3、关注点
publicinterfaceBeanDefinitionRegistryPostProcessorextendsBeanFactoryPostProcessor{/**
* Modify the application context's internal bean definition registry after its
* standard initialization. All regular bean definitions will have been loaded,
* but no beans will have been instantiated yet. This allows for adding further
* bean definitions before the next post-processing phase kicks in.
* @param registry the bean definition registry used by the application context
* @throws org.springframework.beans.BeansException in case of errors
*/voidpostProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)throwsBeansException;}
@FunctionalInterfacepublicinterfaceBeanFactoryPostProcessor{/**
* Modify the application context's internal bean factory after its standard
* initialization. All bean definitions will have been loaded, but no beans
* will have been instantiated yet. This allows for overriding or adding
* properties even to eager-initializing beans.
* @param beanFactory the bean factory used by the application context
* @throws org.springframework.beans.BeansException in case of errors
*/voidpostProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)throwsBeansException;}