invokeBeanFactoryPostProcessors()执行注解
1 public static void invokeBeanFactoryPostProcessors( 2 ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) { 3 4 // Invoke BeanDefinitionRegistryPostProcessors first, if any. 5 Set<String> processedBeans = new HashSet<>(); 6 //先判断是否是beanFactory的实例-都是 7 if (beanFactory instanceof BeanDefinitionRegistry) { 8 BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; 9 List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>(); 10 List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>(); 11 //beanFactoryPostProcessors 是通过addBeanFactoryPostProcessor加进来的,如果实现了BeanDefinitionRegistryPostProcessor, 12 //立马执行postProcessBeanDefinitionRegistry(),优先级最高 13 //区别:BeanDefinitionRegistryPostProcessor用来添加额外的bd,而BeanFactoryPostProcessor用来修改bd 14 for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { 15 if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { 16 BeanDefinitionRegistryPostProcessor registryProcessor = 17 (BeanDefinitionRegistryPostProcessor) postProcessor; 18 registryProcessor.postProcessBeanDefinitionRegistry(registry); 19 registryProcessors.add(registryProcessor); 20 } 21 else { 22 regularPostProcessors.add(postProcessor); 23 } 24 } 25 26 // Do not initialize FactoryBeans here: We need to leave all regular beans 27 // uninitialized to let the bean factory post-processors apply to them! 28 // Separate between BeanDefinitionRegistryPostProcessors that implement 29 // PriorityOrdered, Ordered, and the rest. 30 List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>(); 31 32 // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered. 33 //只有一个类:ConfigurationClassPostProcessor 34 //createApplicationContext会通过反射生成AnnotationConfigApplicationContext实例 35 // AnnotationConfigApplicationContext无参构造函数中会通过RootBeanDefinition来注册ConfigurationClassPostProcessor、 36 // AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、EventListenerMethodProcessor、DefaultEventListenerFactory 37 String[] postProcessorNames = 38 beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); 39 for (String ppName : postProcessorNames) { 40 if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { 41 currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); 42 processedBeans.add(ppName); 43 } 44 } 45 sortPostProcessors(currentRegistryProcessors, beanFactory); 46 registryProcessors.addAll(currentRegistryProcessors); 47 //开始解析ConfigurationClassPostProcessor,到这里才会扫描获取其他bean产生beanDefinitionMap,这个类会解析spring.factories、@Import @CompanScan @Bean 等注解 48 invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); 49 currentRegistryProcessors.clear(); 50 51 // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered. 52 //这里可以看出PriorityOrdered的优先级比Ordered高 53 //重新调用方面获取postProcessorNames的原因: 54 //上面解析ConfigurationClassPostProcessor完成后会增加新的实现BeanDefinitionRegistryPostProcessor的类,所以要重新获取 55 postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); 56 for (String ppName : postProcessorNames) { 57 if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) { 58 currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); 59 processedBeans.add(ppName); 60 } 61 } 62 sortPostProcessors(currentRegistryProcessors, beanFactory); 63 registryProcessors.addAll(currentRegistryProcessors); 64 invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); 65 currentRegistryProcessors.clear(); 66 67 // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear. 68 //之后调用即没有实现PriorityOrdered也没有实现Ordered的BeanDefinitionRegistryPostProcessors类 69 boolean reiterate = true; 70 while (reiterate) { 71 reiterate = false; 72 //需要重新获取postProcessorNames,原因和之前一样 73 postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); 74 for (String ppName : postProcessorNames) { 75 //之前已经解析过的不再解析 76 if (!processedBeans.contains(ppName)) { 77 currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); 78 processedBeans.add(ppName); 79 reiterate = true; 80 } 81 } 82 sortPostProcessors(currentRegistryProcessors, beanFactory); 83 registryProcessors.addAll(currentRegistryProcessors); 84 invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); 85 currentRegistryProcessors.clear(); 86 } 87 88 // Now, invoke the postProcessBeanFactory callback of all processors handled so far. 89 //调用实现BeanDefinitionRegistryPostProcessor的类的postProcessBeanFactory方法 90 invokeBeanFactoryPostProcessors(registryProcessors, beanFactory); 91 //调用由通过addBeanFactoryPostProcessor加进来的实现BeanFactoryPostProcessor接口的类的方法 92 //BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor 所以addBeanFactoryPostProcessor2个接口的实现类都可以加 93 invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); 94 } 95 96 else { 97 // Invoke factory processors registered with the context instance. 98 invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory); 99 } 100 101 // Do not initialize FactoryBeans here: We need to leave all regular beans 102 // uninitialized to let the bean factory post-processors apply to them! 103 //这个之后就会调用直接实现BeanFactoryPostProcessor接口的类,调用规则和上面一样, 104 //先调用实现了PriorityOrdered,再调用Ordered,再调用2个都没有的实现了BeanFactoryPostProcessor接口的类 105 String[] postProcessorNames = 106 beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false); 107 108 // Separate between BeanFactoryPostProcessors that implement PriorityOrdered, 109 // Ordered, and the rest. 110 List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>(); 111 List<String> orderedPostProcessorNames = new ArrayList<>(); 112 List<String> nonOrderedPostProcessorNames = new ArrayList<>(); 113 for (String ppName : postProcessorNames) { 114 if (processedBeans.contains(ppName)) { 115 // skip - already processed in first phase above 116 } 117 else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { 118 priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)); 119 } 120 else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { 121 orderedPostProcessorNames.add(ppName); 122 } 123 else { 124 nonOrderedPostProcessorNames.add(ppName); 125 } 126 } 127 128 // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered. 129 sortPostProcessors(priorityOrderedPostProcessors, beanFactory); 130 invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); 131 132 // Next, invoke the BeanFactoryPostProcessors that implement Ordered. 133 List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size()); 134 for (String postProcessorName : orderedPostProcessorNames) { 135 orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); 136 } 137 sortPostProcessors(orderedPostProcessors, beanFactory); 138 invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); 139 140 // Finally, invoke all other BeanFactoryPostProcessors. 141 List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size()); 142 for (String postProcessorName : nonOrderedPostProcessorNames) { 143 nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); 144 } 145 invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory); 146 147 // Clear cached merged bean definitions since the post-processors might have 148 // modified the original metadata, e.g. replacing placeholders in values... 149 beanFactory.clearMetadataCache(); 150 }