查看全部
// Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory);
- 执行bean factory的后置处理器
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
1.1. 先执行BeanDefinitionRegistryPostProcessors,然后才是其他
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set processedBeans = new HashSet();
1.2. 如果beanFactory实现了BeanDefinitionRegistry接口,则将传入的bean factory后置处理器分为实现了BeanDefinitionRegistryPostProcessor接口的后置处理器和其他后置处理器,并且回调了BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法;
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
regularPostProcessors.add(postProcessor);
}
}
注:BeanDefinitionRegistryPostProcessor的作用是,向bean 定义信息的保存中心注册bean的定义信息,是一种注册bean的方法。
/**
* e.g.: 注册两个bean, 两种方法,一个是beanName为firstBean的Dog,一个是beanName为secondBean的Cat
* @param registry bean定义信息的保存中心 beanFactory就是按照BeanDefinitionRegistry中的定义信息创建bean信息
*/
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Dog.class);
registry.registerBeanDefinition("firstBean", rootBeanDefinition);
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Cat.class).getBeanDefinition();
registry.registerBeanDefinition("secondBean", beanDefinition);
}
1.3、获得所有的BeanDefinitionRegistryPostProcessor,并将其按照实现的接口,排列顺序--PriorityOrdered -> Ordered -> 其他
,且分别回调postProcessBeanDefinitionRegistry
//主要的代码示例
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
//获取到所有的BeanDefinitionRegistryPostProcessor
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
//如果是实现了PriorityOrdered接口的,添加到currentRegistryProcessors
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
//将currentRegistryProcessors排序
sortPostProcessors(currentRegistryProcessors, beanFactory);
//registryProcessors添加所有的后置处理器,以便等会回调其他方法
registryProcessors.addAll(currentRegistryProcessors);
//回调BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
//清空currentRegistryProcessors
currentRegistryProcessors.clear();
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
//...
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
//...
//同样处理Ordered接口的和剩下的实现了BeanDefinitionRegistryPostProcessors的后置处理器
1.4 回调bean factory的postProcessBeanFactory
方法
// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
1.5 如果beanFactory没有实现了BeanDefinitionRegistry接口,则直接回调bean factory的postProcessBeanFactory
方法
1.6 获取所有的BeanFactoryPostProcessor,类似上面步骤,重新分类排序,然后回调postProcessBeanFactory
。
- 处理AspectJ相关