Bootstrap

spring的核心方法只之invokeBeanFactoryPostProcessors

invokeBeanFactoryPostProcessors就是将项目中的BeanDefinition加载进beanDefinitionMap中,并且对BeanFactoryPostProcess的实现类进行回调的地方。

先往spring容器中加入四个扩展类,便于下面演示。

HandFactoryPostProcessor: 手动的往spring容器中加入的BeanFactoryPostProcessor的实现类

public class HandFactoryPostProcessor implements BeanFactoryPostProcessor {
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("HandFactoryPostProcessor ---> postProcessBeanFactory");
	}
}

HandRegisterPostProcessor:手动的往spring容器中加入的BeanDefinitionRegistryPostProcessor的实现类

public class HandRegisterPostProcessor implements BeanDefinitionRegistryPostProcessor {
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		System.out.println("HandRegisterPostProcessor ---> postProcessBeanDefinitionRegistry");
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("HandRegisterPostProcessor ---> postProcessBeanFactory");
	}
}

ScanFactoryPostProcessor:通过注解的方式往spring容器中加入的BeanFactoryPostProcessor的实现类

@Component
public class ScanFactoryPostProcessor implements BeanFactoryPostProcessor {
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("ScanFactoryPostProcessor ---> postProcessBeanFactory");
	}
}

ScanRegisterPostProcessor:通过注解的方式往spring容器中加入的BeanDefinitionRegistryPostProcessor的实现类

@Component
public class ScanRegisterPostProcessor implements BeanDefinitionRegistryPostProcessor {
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		System.out.println("ScanRegisterPostProcessor ---> postProcessBeanDefinitionRegistry");
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("ScanRegisterPostProcessor ---> postProcessBeanFactory");
	}
}

这里说的程序员手动加入是指,在执行spring容器的refresh方法之前就,通过调用容器的接口,手动的往容器中加入BeanDefinitionRegistryPostProcessor接口的实现类的做法,代码如下。

AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext();
// 手动加入HandFactoryPostProcessor和HandRegisterPostProcessor
ioc.addBeanFactoryPostProcessor(new HandFactoryPostProcessor());
ioc.addBeanFactoryPostProcessor(new HandRegisterPostProcessor());
ioc.register(AppConfig.class);
ioc.refresh();

invokeBeanFactoryPostProcessors方法的执行主要包括一下几个部分:

  • 执行程序员手动加入到容器中的BeanDefinitionRegistryPostProcessor接口的实现类。

    源码如下:

    for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
        if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
            BeanDefinitionRegistryPostProcessor registryProcessor =
                (BeanDefinitionRegistryPostProcessor) postProcessor;
            // 此处即为执行手动加入到容器中的BeanDefinitionRegistryPostProcessor接口的实现类。
            registryProcessor.postProcessBeanDefinitionRegistry(registry);
            registryProcessors.add(registryProcessor);
        }
        else {
            // 只实现BeanFactoryPostProcessor接口的扩展接口
            regularPostProcessors.add(postProcessor);
        }
    }
    

    运行完上述源码的控制台结果如下:

    执行了通过手动方式加入到容器中的BeanDefinitionRegistryPostProcessor接口的实现类


  • 调用实现了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor的扩展类

    ps:在这一步所执行的扩展类可以是spring自带的扩展类或是程序员往程序中加入的,平时主要是执行spring自带的扩展类比如ConfigurationClassPostProcessor:用于解析往spring容器中注入BeanDefinition的注解@Configuration, @Bean, @Service, @Component, @Repository, @Import等。

    源代码如下:

    // 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.
    List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
    
    // First, invoke the BeanDefinitionRegistryPostProcessors that implement 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);
    // 为了之后可以不用再去找父类,因为现在实现完BeanDefinitionRegistryPostProcessor中的方法之后,后面还需要用到这个扩展类
    registryProcessors.addAll(currentRegistryProcessors);
    /**
     * 执行实现了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor的扩展类
     */
    invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
    currentRegistryProcessors.clear();
    

    运行到此阶段执行的BeanDefinitionRegistryPostProcessor扩展类如下:

  • 调用实现了Ordered接口的BeanDefinitionRegistryPostProcessor的扩展类。

    源码如下:

    // Next, invoke the BeanDefinitionRegistryPostProcessors that implement 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);
    invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
    currentRegistryProcessors.clear();
    

    执行逻辑与调用实现了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor的扩展类类似。


  • 调用没有实现PriorityOrderedOrdered接口BeanDefinitionRegistryPostProcessor的扩展类。(这一步执行完之后BeanDefinitionRegistryPostProcessor的扩展类都执行完了)

    源码如下:

    // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
    boolean reiterate = true;
    while (reiterate) {
        reiterate = false;
        postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        for (String ppName : postProcessorNames) {
            if (!processedBeans.contains(ppName)) {
                // 找到剩下来的那些还没有被执行过的BeanDefinitionRegistryPostProcessor
                currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                processedBeans.add(ppName);
                reiterate = true;
            }
        }
        sortPostProcessors(currentRegistryProcessors, beanFactory);
        registryProcessors.addAll(currentRegistryProcessors);
        // 到此为止所有的BeanDefinitionRegistryPostProcessor中的扩展口都被执行过了
        invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
        currentRegistryProcessors.clear();
    }
    

    这里调用的BeanDefinitionRegistryPostProcessor接口的实现类如下:

    执行结果:


  • 调用BeanDefinitionRegistryPostProcessor的父类方法(因为BeanDefinitionRegistryPostProcessor接口也是继承了BeanFactoryPostProcessor接口,所以BeanDefinitionRegistryPostProcessor的父类方法就是指的扩展类中的postProcessBeanFactory方法)

    源码如下:

    // registryProcessors 为 List<BeanDefinitionRegistryPostProcessor>,是之前调用的所有的BeanDefinitionRegistryPostProcessor接口的实现类。
    invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
    

    执行结果是:

  • 调用程序员手动加入到容器中的BeanFactoryPostProcessors的扩展类。

    源码如下:

    // regularPostProcessors,为先前手动加入到spring容器中的BeanFactoryPostProcessors接口的实现类
    invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
    

    执行结果:


  • 调用实现了PriorityOrdered接口的BeanFactoryPostProcessors的扩展类。

    源码如下:

    // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
    // Ordered, and the rest.
    List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
    List<String> orderedPostProcessorNames = new ArrayList<>();
    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
    for (String ppName : postProcessorNames) {
        if (processedBeans.contains(ppName)) {
            // skip - already processed in first phase above
        }
        else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
        }
        else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
            orderedPostProcessorNames.add(ppName);
        }
        else {
            nonOrderedPostProcessorNames.add(ppName);
        }
    }
    
    // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
    invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
    

    和上述执行实现了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor的扩展类逻辑一致。


  • 调用实现了Ordered接口的BeanFactoryPostProcessors的扩展类。

    源码如下:

    		// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
    		List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
    		for (String postProcessorName : orderedPostProcessorNames) {
    			orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    		}
    		sortPostProcessors(orderedPostProcessors, beanFactory);
    		invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
    

  • 调用没有实现PriorityOrderedOrdered接口的BeanFactoryPostProcessors的扩展类。

    源码如下:

    // Finally, invoke all other BeanFactoryPostProcessors.
    List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
    for (String postProcessorName : nonOrderedPostProcessorNames) {
        nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    }
    invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
    

    执行结果:

;