链接

invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory)

循环遍历传递进来的beanFactoryPostProcesser, 这个集合一般情况下为空, 除非我们自己调用容器的addBeanFactoryPostProcessor方法;

因为一般是空,所以跳过

  1. public static void invokeBeanFactoryPostProcessors(
  2. ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
  3. xxxx;
  4. // 这个beanFactoryPostProcessors集合一般情况下都是空的,除非我们手动调用容器的addBeanFactoryPostProcessor方法
  5. for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
  6. // BeanDefinitionRegistryPostProcessor是一个特殊的BeanFactoryPostProcessor,需要先执行postProcessBeanDefinitionRegistry方法
  7. if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
  8. BeanDefinitionRegistryPostProcessor registryProcessor =
  9. (BeanDefinitionRegistryPostProcessor) postProcessor;
  10. registryProcessor.postProcessBeanDefinitionRegistry(registry);
  11. registryProcessors.add(registryProcessor);
  12. }
  13. else {
  14. regularPostProcessors.add(postProcessor);
  15. }
  16. }
  17. xxx;
  18. }

然后,拿到ConfigurationClassPostProcessor,。他实现了BeanDefinitionRegistryPostProcessor,判断是否实现了PriorityOrdered接口,如果实现了要最优先调用,排序 -> 添加 -> 调用

  1. public static void invokeBeanFactoryPostProcessors(
  2. ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
  3. xxxx;
  4. String[] postProcessorNames =
  5. beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
  6. for (String ppName : postProcessorNames) {
  7. // 判断这个类是否还实现了PriorityOrdered接口
  8. if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
  9. // 这里调用了getBean,所以生成一个BeanDefinitionRegistryPostProcessor的bean对象
  10. currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
  11. processedBeans.add(ppName);
  12. }
  13. }
  14. // 排序
  15. sortPostProcessors(currentRegistryProcessors, beanFactory);
  16. // 添加
  17. registryProcessors.addAll(currentRegistryProcessors);
  18. // 执行postProcessBeanDefinitionRegistry方法
  19. invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
  20. currentRegistryProcessors.clear();
  21. xxx;
  22. }

然后,添加实现了Order接口的BeanDefinitionRegistryPostProcessors,同样的,排序 - > 添加 ->调用:

  1. public static void invokeBeanFactoryPostProcessors(
  2. ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
  3. xxxx;
  4. String[] postProcessorNames =
  5. beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
  6. for (String ppName : postProcessorNames) {
  7. // 判断这个类是否还实现了PriorityOrdered接口
  8. if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
  9. // 这里调用了getBean,所以生成一个BeanDefinitionRegistryPostProcessor的bean对象
  10. currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
  11. processedBeans.add(ppName);
  12. }
  13. }
  14. sortPostProcessors(currentRegistryProcessors, beanFactory);
  15. registryProcessors.addAll(currentRegistryProcessors);
  16. invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
  17. currentRegistryProcessors.clear();
  18. xxx;
  19. }

最后,普通BeanDefinitionRegistryPostProcessors,

  1. public static void invokeBeanFactoryPostProcessors(
  2. ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
  3. xxxx;
  4. // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
  5. // 最后,最后其他普通的BeanDefinitionRegistryPostProcessors的postProcessBeanDefinitionRegistry方法
  6. boolean reiterate = true;
  7. // 在一个BeanDefinitionRegistryPostProcessor中可以注册另一个BeanDefinitionRegistryPostProcessor,所以需要递归找出所有的BeanDefinitionRegistryPostProcessor
  8. // 一个没有实现PriorityOrdered接口的BeanDefinitionRegistryPostProcessor如果在内部注册了一个实现了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor,那么就是没有实现PriorityOrdered接口的先执行
  9. while (reiterate) {
  10. reiterate = false;
  11. // 这里会再一次拿到实现了PriorityOrdered接口或Ordered接口的BeanDefinitionRegistryPostProcessor,所以需要processedBeans进行过滤
  12. postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
  13. for (String ppName : postProcessorNames) {
  14. if (!processedBeans.contains(ppName)) {
  15. currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
  16. processedBeans.add(ppName);
  17. reiterate = true;
  18. }
  19. }
  20. sortPostProcessors(currentRegistryProcessors, beanFactory);
  21. registryProcessors.addAll(currentRegistryProcessors);
  22. invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
  23. currentRegistryProcessors.clear();
  24. }
  25. xxx;
  26. }

然后要去调用invokeBeanFactoryPostProcessors, 注意BeanDefinitionRegistryPostProcessor继承了BeanFactoryPostProcessor:

  1. */
  2. public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
  3. /**
  4. * Modify the application context's internal bean definition registry after its
  5. * standard initialization. All regular bean definitions will have been loaded,
  6. * but no beans will have been instantiated yet. This allows for adding further
  7. * bean definitions before the next post-processing phase kicks in.
  8. * @param registry the bean definition registry used by the application context
  9. * @throws org.springframework.beans.BeansException in case of errors
  10. */
  11. void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
  12. }

因此对于我们的ConfigurationClassPostProcessor,因为他实现了BeanDefinitionRegistryPostProcessor和PriorityOrdered,因此在第一步和invokeBeanFactoryPostProcessors都会被调用。

invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);

首先,为每个配置类(加了@Configuration)注册了cglib动态代理:

  1. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
  2. xxx;
  3. // 判断是否需要对AppConfig类生成代理对象
  4. enhanceConfigurationClasses(beanFactory);
  5. // Bean的后置处理器
  6. beanFactory.addBeanPostProcessor(new ImportAwareBeanPostProcessor(beanFactory));
  7. }
  8. public void enhanceConfigurationClasses(ConfigurableListableBeanFactory beanFactory) {
  9. xxx;
  10. // 生成AppConfig的代理对象
  11. ConfigurationClassEnhancer enhancer = new ConfigurationClassEnhancer();
  12. }

动态代理类是为了在获取对象时,先从Bean IOC容器中获取,而不是使用自己new出来的对象。

拿到@ComponentScan

  1. protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)
  2. throws IOException {
  3. xxx;
  4. Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
  5. sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
  6. xxx;
  7. }

拿到以后解析,将包名换成物理路径名,进行扫描,得到指定目录下所有的类。

读到所有的类以后,