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

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

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

  四、xml源码解析:obtainFreshBeanFactory

  1)开端

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

  2)refreshBeanFactory

    protected final void refreshBeanFactory() throws BeansException {
if (this.hasBeanFactory()) {
this.destroyBeans();
this.closeBeanFactory();
} try {
//是不是很熟悉,这里也是通过DefaultListableBeanFactory的方式来实现的
DefaultListableBeanFactory beanFactory = this.createBeanFactory();
beanFactory.setSerializationId(this.getId());
//自定义beanFactory,处理循环依赖
//主要作用设置@Autowired和@Qualifier解析器QualifierAnnotationAutowireCandidateResolver
this.customizeBeanFactory(beanFactory);
//xml解析过程
this.loadBeanDefinitions(beanFactory);
Object var2 = this.beanFactoryMonitor;
synchronized(this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
} catch (IOException var4) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + this.getDisplayName(), var4);
}
}

  a、DefaultListableBeanFactory继承关系

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

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

    protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
if (this.allowBeanDefinitionOverriding != null) {
beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding.booleanValue());
} if (this.allowCircularReferences != null) {
beanFactory.setAllowCircularReferences(this.allowCircularReferences.booleanValue());
} beanFactory.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer());
beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
}

  c、loadBeanDefinitions

    protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
//是不是又很熟悉,还是基于XmlBeanDefinitionReader来进行解析了
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
//解析xml
this.initBeanDefinitionReader(beanDefinitionReader);
this.loadBeanDefinitions(beanDefinitionReader);
}
    protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
Resource[] configResources = this.getConfigResources();
if (configResources != null) {
reader.loadBeanDefinitions(configResources);
}
//这里是配置方式不一样,我们是通过configLocations进行配置
String[] configLocations = this.getConfigLocations();
if (configLocations != null) {
//解析文件
reader.loadBeanDefinitions(configLocations);
} }
    public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
Assert.notNull(locations, "Location array must not be null");
int counter = 0;
String[] var6 = locations;
int var5 = locations.length; for(int var4 = 0; var4 < var5; ++var4) {
String location = var6[var4];
       //主要目的计数
counter += this.loadBeanDefinitions(location);
} return counter;
}
  public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
return this.loadBeanDefinitions(location, (Set)null);
} public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
ResourceLoader resourceLoader = this.getResourceLoader();
if (resourceLoader == null) {
throw new BeanDefinitionStoreException("Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
} else {
int loadCount;
if (!(resourceLoader instanceof ResourcePatternResolver)) {
Resource resource = resourceLoader.getResource(location);
loadCount = this.loadBeanDefinitions((Resource)resource);
if (actualResources != null) {
actualResources.add(resource);
} if (this.logger.isDebugEnabled()) {
this.logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
} return loadCount;
} else {
try {
Resource[] resources = ((ResourcePatternResolver)resourceLoader).getResources(location);
//包装成Resource,进行解析
loadCount = this.loadBeanDefinitions(resources);
if (actualResources != null) {
Resource[] var9 = resources;
int var8 = resources.length; for(int var7 = 0; var7 < var8; ++var7) {
Resource resource = var9[var7];
actualResources.add(resource);
}
} if (this.logger.isDebugEnabled()) {
this.logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
} return loadCount;
} catch (IOException var10) {
throw new BeanDefinitionStoreException("Could not resolve bean definition resource pattern [" + location + "]", var10);
}
}
}
}
    public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
Assert.notNull(resources, "Resource array must not be null");
int counter = 0;
Resource[] var6 = resources;
int var5 = resources.length; for(int var4 = 0; var4 < var5; ++var4) {
Resource resource = var6[var4];
//单独解析没一个resource
counter += this.loadBeanDefinitions((Resource)resource);
} return counter;
}

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

public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
return this.loadBeanDefinitions(new EncodedResource(resource));
} public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
Assert.notNull(encodedResource, "EncodedResource must not be null");
if (this.logger.isInfoEnabled()) {
this.logger.info("Loading XML bean definitions from " + encodedResource.getResource());
} Set<EncodedResource> currentResources = (Set)this.resourcesCurrentlyBeingLoaded.get();
if (currentResources == null) {
currentResources = new HashSet(4);
this.resourcesCurrentlyBeingLoaded.set(currentResources);
} if (!((Set)currentResources).add(encodedResource)) {
throw new BeanDefinitionStoreException("Detected cyclic loading of " + encodedResource + " - check your import definitions!");
} else {
int var6;
try {
InputStream inputStream = encodedResource.getResource().getInputStream(); try {
InputSource inputSource = new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
} var6 = this.doLoadBeanDefinitions(inputSource, encodedResource.getResource());
} finally {
inputStream.close();
}
} catch (IOException var15) {
throw new BeanDefinitionStoreException("IOException parsing XML document from " + encodedResource.getResource(), var15);
} finally {
((Set)currentResources).remove(encodedResource);
if (((Set)currentResources).isEmpty()) {
this.resourcesCurrentlyBeingLoaded.remove();
} } return var6;
}
}

  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. 体验了Sublime + Emmet,才体会到原来前端开发可以这么痛快!

    从当初用notepad写出第一个web页面,到现在偶尔使用Editplus做一些HTML5的消遣,不知不觉已经15年了  --! 在这中间,和那些老顽固一样,坚决远离FP.DW那些半自动的前端开发工具 ...

  2. C/C++心得-结构体

    先说句题外话,个人认为,基本上所有的高级语言被设计出来的最终目的是降低软件开发难度,提升软件开发人员素质和团队协作能力,降低软件维护的难度.在学习语言的时候,可以从这么方面来推测各种语言语法设计的原因 ...

  3. SSM框架之多数据源配置

    多数据源的应用场景:主要是数据库拆分后,怎样让多个数据库结合起来来达到业务需求. SSM框架(Spring+SpringMVC+MyBatis(MyBatis-Plus))是目前最常用的,此次仍然是m ...

  4. n维向量旋转(循环移位)——学习《编程珠玑》

    问题: 将一个n元一维向量向左旋转i个位置.例如,当n=8且i=3时,向量abcdefgh旋转为defghabc. 简单的代码使用一个n元的中间向量在n步内完成该工作. 你能否仅使用数十个额外字节的存 ...

  5. Spring通过注解装配Bean

    通过注解实现ServiceImpl业务 一.使用@Component装配Bean 1. 定义类:User 在类上面加@Component注解,在属性上面加@Value值 package com.wbg ...

  6. 【题解】洛谷P3959 [NOIP2017TG] 宝藏(状压DP+DFS)

    洛谷P3959:https://www.luogu.org/problemnew/show/P3959 前言 NOIP2017时还很弱(现在也很弱 看出来是DP 但是并不会状压DP 现在看来思路并不复 ...

  7. CMarkup成员方法简介 (转)

    CMarkup成员方法简介 (转) 转自:http://blog.csdn.net/magictong/article/details/6669837翻译:magictong(童磊)2011年7月版权 ...

  8. [node.js学习]为node.js写的一个操作mysql的类

    不支持一个对象在不同异步中使用,模仿的php框架 speedphp中的model模块 GaryMysql.js var mysql = require('mysql'); var pool = nul ...

  9. ABAP术语-Sales Document

    Sales Document 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/13/1103294.html Data base docume ...

  10. Zabbix——解决中文显示乱码

    前提条件: 准备好上传工具,我用的是WinSCP 使用字体是微软雅黑,如果使用其他的更改名称即可. 更改Zabbix服务器的默认参数: vi /usr/share/zabbix/include/def ...