查看全部
// Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory);
- 執(zhí)行bean factory的后置處理器
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
1.1. 先執(zhí)行BeanDefinitionRegistryPostProcessors,然后才是其他
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set<String> processedBeans = new HashSet<String>();
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中的定義信息創(chuàng)建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相關