spring源码学习笔记-初始化(二) PostProcessor
来源:互联网 发布:佳能ip1180打印机端口 编辑:程序博客网 时间:2024/05/16 02:02
下面我们重点查看这个方法:
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory)
这个方法主要是实例化和反调所有注册的BeanFactoryProcessor 对象集,必须调用在单个实例之前。
// Invoke BeanDefinitionRegistryPostProcessors first, if any.Set<String> processedBeans = new HashSet<String>();if (beanFactory instanceof BeanDefinitionRegistry) {BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>();List<BeanDefinitionRegistryPostProcessor> registryPostProcessors =new LinkedList<BeanDefinitionRegistryPostProcessor>();for (BeanFactoryPostProcessor postProcessor : getBeanFactoryPostProcessors()) {if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {BeanDefinitionRegistryPostProcessor registryPostProcessor =(BeanDefinitionRegistryPostProcessor) postProcessor;registryPostProcessor.postProcessBeanDefinitionRegistry(registry);registryPostProcessors.add(registryPostProcessor);}else {regularPostProcessors.add(postProcessor);}}Map<String, BeanDefinitionRegistryPostProcessor> beanMap =beanFactory.getBeansOfType(BeanDefinitionRegistryPostProcessor.class, true, false);List<BeanDefinitionRegistryPostProcessor> registryPostProcessorBeans =new ArrayList<BeanDefinitionRegistryPostProcessor>(beanMap.values());OrderComparator.sort(registryPostProcessorBeans);for (BeanDefinitionRegistryPostProcessor postProcessor : registryPostProcessorBeans) {postProcessor.postProcessBeanDefinitionRegistry(registry);}invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory);invokeBeanFactoryPostProcessors(registryPostProcessorBeans, beanFactory);invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);processedBeans.addAll(beanMap.keySet());}else {// Invoke factory processors registered with the context instance.invokeBeanFactoryPostProcessors(getBeanFactoryPostProcessors(), beanFactory);}
上塑造型,把beanFactory上溯到BeanDefinitionRegistry接口,第5行代码创建一个LinkedList集合,里面存放BeanFactoryPostProcessor对象。第6行代码
创建一个LinkList集合,registryPostProcessors用来存放一些用来对bean定义的注册信息进行处理的PostProcessor。
第8行循环对普通的PostProcessor进行循环;第9行代码判断postProcessor是否为对bean定义的注册信息进行处理的PostProcessor,
如果是的话,对其进行相应的业务处理,否则处理普通的postProcessor.
代码二:
Map<String, BeanDefinitionRegistryPostProcessor> beanMap =beanFactory.getBeansOfType(BeanDefinitionRegistryPostProcessor.class, true, false);List<BeanDefinitionRegistryPostProcessor> registryPostProcessorBeans =new ArrayList<BeanDefinitionRegistryPostProcessor>(beanMap.values());OrderComparator.sort(registryPostProcessorBeans);for (BeanDefinitionRegistryPostProcessor postProcessor : registryPostProcessorBeans) {postProcessor.postProcessBeanDefinitionRegistry(registry);}invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory);invokeBeanFactoryPostProcessors(registryPostProcessorBeans, beanFactory);invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);processedBeans.addAll(beanMap.keySet());
第1行是对bean定义注册信息进行处理的PostProcessor的beanType的获取,第3行通过beanMaps得到registryPostProcessorBeans;
第5行调用排序方法对registryPostProcessorBeans进行排序,第6-8行调用registryPostProcessorBeans,
第9-12行,调用invokeBeanFactoryPostPrcessors方法,就是为每个PostProcessor,进行postProcessor.postProcessBeanFactory()方法调用。
13行,processedBeans存放所有BeanDefinitionRegistryPostProcessor类型的对象。
代码三:
// Do not initialize FactoryBeans here: We need to leave all regular beans// uninitialized to let the bean factory post-processors apply to them!String[] postProcessorNames =beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,// Ordered, and the rest.List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();List<String> orderedPostProcessorNames = new ArrayList<String>();List<String> nonOrderedPostProcessorNames = new ArrayList<String>();for (String ppName : postProcessorNames) {if (processedBeans.contains(ppName)) {// skip - already processed in first phase above}else if (isTypeMatch(ppName, PriorityOrdered.class)) {priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));}else if (isTypeMatch(ppName, Ordered.class)) {orderedPostProcessorNames.add(ppName);}else {nonOrderedPostProcessorNames.add(ppName);}}// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.OrderComparator.sort(priorityOrderedPostProcessors);invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);// Next, invoke the BeanFactoryPostProcessors that implement Ordered.List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();for (String postProcessorName : orderedPostProcessorNames) {orderedPostProcessors.add(getBean(postProcessorName, BeanFactoryPostProcessor.class));}OrderComparator.sort(orderedPostProcessors);invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);// Finally, invoke all other BeanFactoryPostProcessors.List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();for (String postProcessorName : nonOrderedPostProcessorNames) {nonOrderedPostProcessors.add(getBean(postProcessorName, BeanFactoryPostProcessor.class));}invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
- spring源码学习笔记-初始化(二) PostProcessor
- spring源码学习笔记-初始化(四)-PostProcessor
- spring源码学习笔记-初始化(四)-PostProcessor
- spring源码学习笔记-初始化(四)-PostProcessor
- spring源码学习笔记-初始化(二)-BeanFactory
- spring源码学习笔记-初始化(二)-BeanFactory
- spring源码学习笔记-初始化(二)-BeanFactory
- spring源码学习笔记-初始化(4)
- 【Spring】IOC核心源码学习(二):容器初始化过程
- 【Spring】IOC核心源码学习(二):容器初始化过程
- Spring源码学习--Bean对象初始化(二)
- spring源码学习笔记-初始化(一)-概览
- spring源码学习笔记-初始化(三)-BeanFactory
- spring源码学习笔记-初始化(五)-MessageSource/事件监听器
- spring源码学习笔记-初始化(三) registerBeanPostProcessors
- spring源码学习笔记-初始化(六) ClassPathXmlApplicationContext
- spring源码学习笔记-初始化(一)-概览
- spring源码学习笔记-初始化(三)-BeanFactory
- 不插usb声卡时,录像机无法关闭
- iPhone/iOS UIViewController之UITabBarController
- 层次聚类算法
- android 如何获取launcher的workspace中的每一个celllayout的位图???
- Myeclipse 默认字符集设置
- spring源码学习笔记-初始化(二) PostProcessor
- 《浅谈VC++工程的文件组织》关于VS的IDE的转载
- 《#诗盗#·快撸》
- 暂无
- CDC的几种用法
- FI/CO基本配置Tcode
- arcgis server 发布图片质量
- 反射对比两个实体。
- 艾迪神总结的VS IDE使用(很文艺的说)