一、ApplicationContext的xml解析工作是通过ClassPathXmlApplicationContext来实现的,其实看过ClassPathXmlApplicationContext的xml源码解析过程的其实都应该知道,xml的解析过程基本上就是用原来的XmlBeanFactory的解析过程完成的。

  二、为什么要拿出来单独说一下呢,其目的就是,了解怎么用同样的方式来实现xml解析过程。

  三、xml解析过程是通过refresh下的obtainFreshBeanFactory函数实现,所以这里重点讲解obtainFreshBeanFactory的实现过程。

  四、xml源码解析:obtainFreshBeanFactory

  1)开端

  1. protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
  2. //解析过程
  3. this.refreshBeanFactory();
  4. //这里的beanFactory就是DefaultListableBeanFactory(后续会讲到)
  5. ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
  6. if (this.logger.isDebugEnabled()) {
  7. this.logger.debug("Bean factory for " + this.getDisplayName() + ": " + beanFactory);
  8. }
  9. return beanFactory;
  10. }

  2)refreshBeanFactory

  1. protected final void refreshBeanFactory() throws BeansException {
  2. if (this.hasBeanFactory()) {
  3. this.destroyBeans();
  4. this.closeBeanFactory();
  5. }
  6.  
  7. try {
  8. //是不是很熟悉,这里也是通过DefaultListableBeanFactory的方式来实现的
  9. DefaultListableBeanFactory beanFactory = this.createBeanFactory();
  10. beanFactory.setSerializationId(this.getId());
  11. //自定义beanFactory,处理循环依赖
  12. //主要作用设置@Autowired和@Qualifier解析器QualifierAnnotationAutowireCandidateResolver
  13. this.customizeBeanFactory(beanFactory);
  14. //xml解析过程
  15. this.loadBeanDefinitions(beanFactory);
  16. Object var2 = this.beanFactoryMonitor;
  17. synchronized(this.beanFactoryMonitor) {
  18. this.beanFactory = beanFactory;
  19. }
  20. } catch (IOException var4) {
  21. throw new ApplicationContextException("I/O error parsing bean definition source for " + this.getDisplayName(), var4);
  22. }
  23. }

  a、DefaultListableBeanFactory继承关系

  这里其实就可以看出ConfigurableListableBeanFactory就是这里的DefaultListableBeanFactory

  b、customizeBeanFactory(这里不详细介绍注解的解析过程)

  1. protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
  2. if (this.allowBeanDefinitionOverriding != null) {
  3. beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding.booleanValue());
  4. }
  5.  
  6. if (this.allowCircularReferences != null) {
  7. beanFactory.setAllowCircularReferences(this.allowCircularReferences.booleanValue());
  8. }
  9.  
  10. beanFactory.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());
  11. beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
  12. }

  c、loadBeanDefinitions

  1. protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
  2. //是不是又很熟悉,还是基于XmlBeanDefinitionReader来进行解析了
  3. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
  4. beanDefinitionReader.setResourceLoader(this);
  5. beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
  6. //解析xml
  7. this.initBeanDefinitionReader(beanDefinitionReader);
  8. this.loadBeanDefinitions(beanDefinitionReader);
  9. }
  1. protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
  2. Resource[] configResources = this.getConfigResources();
  3. if (configResources != null) {
  4. reader.loadBeanDefinitions(configResources);
  5. }
  6. //这里是配置方式不一样,我们是通过configLocations进行配置
  7. String[] configLocations = this.getConfigLocations();
  8. if (configLocations != null) {
  9. //解析文件
  10. reader.loadBeanDefinitions(configLocations);
  11. }
  12.  
  13. }
  1. public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
  2. Assert.notNull(locations, "Location array must not be null");
  3. int counter = 0;
  4. String[] var6 = locations;
  5. int var5 = locations.length;
  6.  
  7. for(int var4 = 0; var4 < var5; ++var4) {
  8. String location = var6[var4];
           //主要目的计数
  9. counter += this.loadBeanDefinitions(location);
  10. }
  11.  
  12. return counter;
  13. }
  1.   public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
  2. return this.loadBeanDefinitions(location, (Set)null);
  3. }
  4.  
  5. public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
  6. ResourceLoader resourceLoader = this.getResourceLoader();
  7. if (resourceLoader == null) {
  8. throw new BeanDefinitionStoreException("Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
  9. } else {
  10. int loadCount;
  11. if (!(resourceLoader instanceof ResourcePatternResolver)) {
  12. Resource resource = resourceLoader.getResource(location);
  13. loadCount = this.loadBeanDefinitions((Resource)resource);
  14. if (actualResources != null) {
  15. actualResources.add(resource);
  16. }
  17.  
  18. if (this.logger.isDebugEnabled()) {
  19. this.logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
  20. }
  21.  
  22. return loadCount;
  23. } else {
  24. try {
  25. Resource[] resources = ((ResourcePatternResolver)resourceLoader).getResources(location);
  26. //包装成Resource,进行解析
  27. loadCount = this.loadBeanDefinitions(resources);
  28. if (actualResources != null) {
  29. Resource[] var9 = resources;
  30. int var8 = resources.length;
  31.  
  32. for(int var7 = 0; var7 < var8; ++var7) {
  33. Resource resource = var9[var7];
  34. actualResources.add(resource);
  35. }
  36. }
  37.  
  38. if (this.logger.isDebugEnabled()) {
  39. this.logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
  40. }
  41.  
  42. return loadCount;
  43. } catch (IOException var10) {
  44. throw new BeanDefinitionStoreException("Could not resolve bean definition resource pattern [" + location + "]", var10);
  45. }
  46. }
  47. }
  48. }
  1. public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
  2. Assert.notNull(resources, "Resource array must not be null");
  3. int counter = 0;
  4. Resource[] var6 = resources;
  5. int var5 = resources.length;
  6.  
  7. for(int var4 = 0; var4 < var5; ++var4) {
  8. Resource resource = var6[var4];
  9. //单独解析没一个resource
  10. counter += this.loadBeanDefinitions((Resource)resource);
  11. }
  12.  
  13. return counter;
  14. }

  d、loadBeanDefinitions(这里是不是很熟悉,这里的解析过程就是XmlBeanFactory里面的xml的核心解析过程)

  1. public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
  2. return this.loadBeanDefinitions(new EncodedResource(resource));
  3. }
  4.  
  5. public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
  6. Assert.notNull(encodedResource, "EncodedResource must not be null");
  7. if (this.logger.isInfoEnabled()) {
  8. this.logger.info("Loading XML bean definitions from " + encodedResource.getResource());
  9. }
  10.  
  11. Set<EncodedResource> currentResources = (Set)this.resourcesCurrentlyBeingLoaded.get();
  12. if (currentResources == null) {
  13. currentResources = new HashSet(4);
  14. this.resourcesCurrentlyBeingLoaded.set(currentResources);
  15. }
  16.  
  17. if (!((Set)currentResources).add(encodedResource)) {
  18. throw new BeanDefinitionStoreException("Detected cyclic loading of " + encodedResource + " - check your import definitions!");
  19. } else {
  20. int var6;
  21. try {
  22. InputStream inputStream = encodedResource.getResource().getInputStream();
  23.  
  24. try {
  25. InputSource inputSource = new InputSource(inputStream);
  26. if (encodedResource.getEncoding() != null) {
  27. inputSource.setEncoding(encodedResource.getEncoding());
  28. }
  29.  
  30. var6 = this.doLoadBeanDefinitions(inputSource, encodedResource.getResource());
  31. } finally {
  32. inputStream.close();
  33. }
  34. } catch (IOException var15) {
  35. throw new BeanDefinitionStoreException("IOException parsing XML document from " + encodedResource.getResource(), var15);
  36. } finally {
  37. ((Set)currentResources).remove(encodedResource);
  38. if (((Set)currentResources).isEmpty()) {
  39. this.resourcesCurrentlyBeingLoaded.remove();
  40. }
  41.  
  42. }
  43.  
  44. return var6;
  45. }
  46. }

  e、剩下的部分参考:https://www.cnblogs.com/ll409546297/p/9451784.html的源码解析部分第4)点

  五、分析一下流程

  1)通过ClassPathXmlApplicationContext来进行xml的解析工作

  2)首先进行赋值到configLocations的属性上面

  3)xml解析过程还是通过XmlBeanDefinitionReader来实现解析

  4)xml解析不同的地方:

  a、新增解析增加计数

  b、返回类型使用接口ConfigurableListableBeanFactory

  c、增加string的配置方式,代码编写感觉更好。

  d、解析之前和之后,都做了很多其他工作。相比之下拓展了很多其他功能。

spring源码-增强容器xml解析-3.1的更多相关文章

  1. Spring源码追踪2——xml解析入口

    解析xml节点入口 org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDe ...

  2. Spring源码学习-容器BeanFactory(四) BeanDefinition的创建-自定义标签的解析.md

    写在前面 上文Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签对Spring默认标签的解析做了详解,在xml元素的解析中,Spri ...

  3. Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签

    写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...

  4. Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作

    写在前面 上文 Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件主要讲Spring容器创建时通过XmlBeanDefinitionReader读 ...

  5. Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件

    写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...

  6. Spring源码-IOC部分-Xml Bean解析注册过程【3】

    实验环境:spring-framework-5.0.2.jdk8.gradle4.3.1 Spring源码-IOC部分-容器简介[1] Spring源码-IOC部分-容器初始化过程[2] Spring ...

  7. spring源码学习五 - xml格式配置,如何解析

    spring在注入bean的时候,可以通过bean.xml来配置,在xml文件中配置bean的属性,然后spring在refresh的时候,会去解析xml配置文件,这篇笔记,主要来记录.xml配置文件 ...

  8. Spring源码系列——容器的启动过程(一)

    一. 前言 Spring家族特别庞大,对于开发人员而言,要想全面征服Spring家族,得花费不少的力气.俗话说,打蛇打七寸,那么Spring家族的"七寸"是什么呢?我心目中的答案一 ...

  9. Spring源码 04 IOC XML方式

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

随机推荐

  1. hdu 3068 最长回文_Manacher模板

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/neng18/article/details/24269469 pid=3068" rel= ...

  2. BZOJ1770:[USACO]lights 燈(高斯消元,DFS)

    Description 貝希和她的閨密們在她們的牛棚中玩遊戲.但是天不從人願,突然,牛棚的電源跳閘了,所有的燈都被關閉了.貝希是一個很膽小的女生,在伸手不見拇指的無盡的黑暗中,她感到驚恐,痛苦與絕望. ...

  3. 2019.1.4 SSH框架整合步骤(一)

    SSH整合 1.三大框架整合原理 Spring与Struts2整合就是将Action对象交给Spring容器负责创建 Spring与Hibernate整合就是将sessionFactory交给Spri ...

  4. java GC是何时对什么东西做什么事情

    之前学习了javaGC的原理机制,有了一定的了解,现在做一个整理总结,便于理解记忆,包括三个问题: 1. java GC是什么时候做的? 2. java GC作用的东西是什么? 3. java GC具 ...

  5. react系列(二)高阶组件-HOC

    高阶组件 简单来说,高阶组件可以看做一个函数,且该函数接受一个组件作为参数,并返回一个新的组件. 我在之前的博客<闭包和类>中提到一个观点,面向对象的好处就在于,易于理解,方便维护和复用. ...

  6. Knowledge Point 20180305 Java程序员详述编码Unicode

    Unicode Unicode(统一码.万国码.单一码)是计算机科学领域里的一项业界标准,包括字符集.编码方案等.Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设 ...

  7. python3带tkinter窗口的ftp服务器,并使用pyinstaller打包成exe

    python带tkinter窗口的ftp服务器,使用python3编写,打包使用pyinstaller,命令 pyinstaller -F .\ftpserver.py 代码也可在我的github上下 ...

  8. 20181029NOIP模拟赛T3

    3 .空间活动 [题目描述] 贝茜和佩奇正在玩一款游戏,在游戏开始会生成一个有n个点m条单向边的地图,经过每条边需要花费价格为Hi的费用(Hi<=1000).但是如果两个点可以互相到达,那么这两 ...

  9. php 当不确定用户输入的是浮点 还是整数 还是字符串时

    $price = (floatval($price))?intval(floatval($price)*100)/100:0;

  10. 鸟哥私房菜笔记-1 (S0_S3)

    学新东西,总感觉还是看书来的实在,直接看教程上手太慢且没有目的, 接下来先啃鸟哥这本吧,买的第三版,内容在鸟哥站上都有: 鸟哥的 Linux 私房菜 -- 基础学习篇目录 (繁体) http://cn ...