protectedvoidinvokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory){//处理实现了BeanFactoryPostProcessor接口的实现类 即bean工厂后置处理器//getBeanFactoryPostProcessors() 这里获取是一个空集合 只是注册(初始化了)到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)){//向容器中添加一个LoadTimeWeaverAwareProcessor后置处理器,用于处理实现了LoadTimeWeaver接口的类 这个是动态代理相关的 织入
beanFactory.addBeanPostProcessor(newLoadTimeWeaverAwareProcessor(beanFactory));//设置一个临时类加载器
beanFactory.setTempClassLoader(newContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));}}
publicstaticvoidinvokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory,List<BeanFactoryPostProcessor> beanFactoryPostProcessors){// Invoke BeanDefinitionRegistryPostProcessors first, if any.//创建一个空集合 存储 实现了BeanDefinitionRegistryPostProcessor,BeanFactoryPostProcessor接口的实现类的名字Set<String> processedBeans =newHashSet<>();if(beanFactory instanceofBeanDefinitionRegistry){//beanFactory 用于获取beanBeanDefinitionRegistry registry =(BeanDefinitionRegistry) beanFactory;//存放实现了BeanFactoryPostProcessor接口的beanList<BeanFactoryPostProcessor> regularPostProcessors =newArrayList<>();//存放实现了BeanDefinitionRegistryPostProcessor接口的beanList<BeanDefinitionRegistryPostProcessor> registryProcessors =newArrayList<>();//beanFactoryPostProcessors 这个集合是空for(BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors){if(postProcessor instanceofBeanDefinitionRegistryPostProcessor){BeanDefinitionRegistryPostProcessor registryProcessor =(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);}else{
regularPostProcessors.add(postProcessor);}}// 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.//初始化一个临时存放bean的集合List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors =newArrayList<>();// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.//获取实现了BeanDefinitionRegistryPostProcessors接口的实现类并且是实现类还实现了PriorityOrdered接口(排序)String[] 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);//执行符合当前规则所有后置处理器 postProcessBeanDefinitionRegistry 方法invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);//清除当前临时集合数据
currentRegistryProcessors.clear();// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.//获取实现了BeanDefinitionRegistryPostProcessors接口的实现类并且是实现类还实现了Ordered接口(排序)
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class,true,false);for(String ppName : postProcessorNames){if(!processedBeans.contains(ppName)&& beanFactory.isTypeMatch(ppName,Ordered.class)){
currentRegistryProcessors.add(beanFactory.getBean(ppName,BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);}}//排序sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);//执行符合当前规则所有后置处理器 postProcessBeanDefinitionRegistry 方法invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);//清除当前临时集合数据
currentRegistryProcessors.clear();// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.boolean reiterate =true;while(reiterate){
reiterate =false;//这里再次获取实现了BeanDefinitionRegistryPostProcessor接口的bean,并且过滤之前已经 处理过的bean
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);//执行符合当前规则所有后置处理器 postProcessBeanDefinitionRegistry 方法invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);//清除当前临时数据
currentRegistryProcessors.clear();}// Now, invoke the postProcessBeanFactory callback of all processors handled so far.//最后执行实现 BeanFactoryPostProcessor 接口中的 postProcessBeanFactoryinvokeBeanFactoryPostProcessors(registryProcessors, beanFactory);invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);}else{// Invoke factory processors registered with the context instance.//如果beanFacgory没有实现BeanDefinitionRegister接口 则直接执行beanFactory后置处理器,循环调用postProcessBeanFactory方法invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);}// Do not initialize FactoryBeans here: We need to leave all regular beans// uninitialized to let the bean factory post-processors apply to them!//获取实现了BeanFactoryPostProcessor接口的beanString[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class,true,false);// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,// Ordered, and the rest.//存放BeanFactoryPostProcessor接口的实现类集合(实现类还要实现了PriorityOrdered接口)List<BeanFactoryPostProcessor> priorityOrderedPostProcessors =newArrayList<>();//存放BeanFactoryPostProcessor接口的实现类名称(实现类实现了Ordered接口)List<String> orderedPostProcessorNames =newArrayList<>();//存放BeanFactoryPostProcessor接口的实现类名称(实现类没有实现排序接口)List<String> nonOrderedPostProcessorNames =newArrayList<>();for(String ppName : postProcessorNames){if(processedBeans.contains(ppName)){// skip - already processed in first phase above}elseif(beanFactory.isTypeMatch(ppName,PriorityOrdered.class)){
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName,BeanFactoryPostProcessor.class));}elseif(beanFactory.isTypeMatch(ppName,Ordered.class)){
orderedPostProcessorNames.add(ppName);}else{
nonOrderedPostProcessorNames.add(ppName);}}// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.//这里是处理实现了PriorityOrdered接口的bean//对于实现了PriorityOrdered接口的bean进行排序sortPostProcessors(priorityOrderedPostProcessors, beanFactory);//最后执行实现 BeanFactoryPostProcessor 接口中的 postProcessBeanFactoryinvokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);// Next, invoke the BeanFactoryPostProcessors that implement Ordered.//这里是处理实现了Ordered接口的bean 同上List<BeanFactoryPostProcessor> orderedPostProcessors =newArrayList<>();for(String postProcessorName : orderedPostProcessorNames){
orderedPostProcessors.add(beanFactory.getBean(postProcessorName,BeanFactoryPostProcessor.class));}sortPostProcessors(orderedPostProcessors, beanFactory);invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);//最后处理没有实现排序接口的bean// Finally, invoke all other BeanFactoryPostProcessors.List<BeanFactoryPostProcessor> nonOrderedPostProcessors =newArrayList<>();for(String postProcessorName : nonOrderedPostProcessorNames){
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName,BeanFactoryPostProcessor.class));}invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);// Clear cached merged bean definitions since the post-processors might have// modified the original metadata, e.g. replacing placeholders in values...//清理缓存
beanFactory.clearMetadataCache();}