记录并分享一下本人学习spring源码的过程,有什么问题或者补充会持续更新。欢迎大家指正!

环境: spring5.X + idea

Spring 是一个工厂,是一个负责对象的创建和维护的工厂。它给我提供了一个功能齐全而且方便我们使用的ApplicationContext子接口,它最底层的接口是BeanFactory。在这个BeanFactory下面衍生了各种功能的子接口。

  1. 容器管理HierarchicalBeanFatory
  2. 自动注入AutowireCapableBeanFactory
  3. 读取配置信息ListableBeanFactory

可以自行找一下BeanFactory类关系图,它有一个子实现类XmlBeanFactory,先说一下XML配置文件的读取

ListableBeanFactory 是读取配置信息的,它的子实现类XmlBeanFactory就是读取xml文件的具体实现。
而ApplicationContext继承了ListableBeanFactory并对xml解析做了进一步的封装所以再我们使用ApplicationContext
时直接给他一个对应位置的资源文件名它就会帮我们读取到配置信息。 ApplicationContext ctx = new ClassPathXmlApplicatiionContext("applicationContext.xml");
User user = ctx.getBean("user"); //我们直接用最底层的接口 BeanFactory 获取xml信息 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
User user = bf.getBean("user");

说明:

  1. beanFactory 底层获取xml文件信息的实现类 XmlBeanFactory 需要传入一个 ClassPathResource 对象。这个对象的父接口就是InputStreamSource 他就是提供了一个获取输入流的方法
    InputStream getInputStream() throws IOException;

    而ClassPathResource 对这个方法的实现就是通过类或者类加载器实现的

    if (this.clazz != null) {
    is = this.clazz.getResourceAsStream(this.path);
    }
    else if (this.classLoader != null) {
    is = this.classLoader.getResourceAsStream(this.path);
    }

    获取了输入流那就自然可以获取文件的内容了。

  2. Spring获取xml内容后通过XmlBeanDefinitionReader解析配置文件的内容封装成BeanDefinition方便后续使用。
    //前边说了是XmlBeanFactory具体实现获取xml信息的功能
    1. public class XmlBeanFactory extends DefaultListableBeanFactory {
    //xmlBeanFactory 中直接实例化 xmlBeanDefinitionReader
    private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
    .....
    public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
    super(parentBeanFactory);
    this.reader.loadBeanDefinitions(resource);
    }
    }
    2. public class XmlBeandefinitionReader extends AbstractBeanDefinitionReader{
    public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
    ......
    try (
    //获取输入流
    InputStream inputStream = encodedResource.getResource().getInputStream()) {
    //xml 解析工具类
    InputSource inputSource = new InputSource(inputStream);
    if (encodedResource.getEncoding() != null) {
    inputSource.setEncoding(encodedResource.getEncoding());
    }
    //开始具体解析
    return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
    }
    ......
    }; protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
    throws BeanDefinitionStoreException {
    try {
    // xml 解析为 document
    Document doc = doLoadDocument(inputSource, resource);
    // document 转 beanDefinition
    int count = registerBeanDefinitions(doc, resource);
    if (logger.isDebugEnabled()) {
    logger.debug("Loaded " + count + " bean definitions from " + resource);
    }
    return count;
    }
    }
    }
    3. public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocumentReader {
    protected void doRegisterBeanDefinitions(Element root){
    ...
    preProcessXml(root);
    parseBeanDefinitions(root,this,dalegate);
    postProcessXml(root);
    }; protected void parseBeanDefinitions(Element root,BeanDefinitionParseDelegate delegate){
    if (delegate.isDefaultNamespace(root)) {
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    if (node instanceof Element) {
    Element ele = (Element) node;
    if (delegate.isDefaultNamespace(ele)) {
    //解析基本标签
    parseDefaultElement(ele, delegate);
    }
    else {
    //解析自定义标签
    delegate.parseCustomElement(ele);
    }
    }
    }
    }
    } }

    解释说明:

    1. 通过 XmlBeanDefinitionReader 的 loadBeanDefinitions 方法得到输入流和xml解析工具类在 doLoadBeanDefinitions方法中把输入流也就是获得的xml文件信息转化为 Document 再通过 registerBeanDefinitions 方法封装成 beanDefinition
    2. 封装beanDefinition是在 DefaultBeanDefinitionDocumentReader 类中的doRegisterBeanDefinitions . parseBeanDefinitions方法做了具体功能的实现,也就是解析文件中的标签并和beanDefinition的属性做映射。例如: 根标签(beans、prefile等) 子标签 (基本标签 bean、import、alias等,自定义标签 aop、mvc:annotation-driven、tx:annotation-driven、context:等)
    3. 用 BeanDefinitionParserDelegate 把解析标签得到的值映射成beanDefinition方便后续使用。

最后

感谢您的阅读,有什么意见和问题欢迎评论区留言!书写不易!

觉得文章对你有帮助记得给我点个赞,欢迎大家关注和转发文章!

干货分享之Spring框架源码解析01-(xml配置解析)的更多相关文章

  1. 干货分享之spring框架源码分析02-(对象创建or生命周期)

    记录并分享一下本人学习spring源码的过程,有什么问题或者补充会持续更新.欢迎大家指正! 环境: spring5.X + idea 之前分析了Spring读取xml文件的所有信息封装成beanDef ...

  2. Spring AMQP 源码分析 08 - XML 配置

    ### 准备 ## 目标 通过 XML 配置文件使用 Spring AMQP ## 前置知识 <Spring AMQP 源码分析 07 - MessageListenerAdapter> ...

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

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

  4. Spring框架源码干货分享之三级缓存和父子工厂

    记录并分享一下本人学习spring源码的过程,有什么问题或者补充会持续更新.欢迎大家指正! 环境: spring5.X + idea 建议:学习过程中要开着源码一步一步过 Spring中对象的创建宏观 ...

  5. Spring框架源码阅读之Springs-beans(一)容器的基本实现概述(待续)

    去年通过实际框架代码的阅读,以及结合<Spring源码深度解析>和<Spring技术内幕>的阅读,对Spring框架内Bean模块有了一个整体性的认识.对此进行的总结性整理和回 ...

  6. spring beans源码解读之--bean definiton解析器

    spring提供了有两种方式的bean definition解析器:PropertiesBeanDefinitionReader和XmLBeanDefinitionReader即属性文件格式的bean ...

  7. Spring MVC源码(四) ----- 统一异常处理原理解析

    SpringMVC除了对请求URL的路由处理特别方便外,还支持对异常的统一处理机制,可以对业务操作时抛出的异常,unchecked异常以及状态码的异常进行统一处理.SpringMVC既提供简单的配置类 ...

  8. 设计模式(五)——原型模式(加Spring框架源码分析)

    原型模式 1 克隆羊问题 现在有一只羊 tom,姓名为: tom, 年龄为:1,颜色为:白色,请编写程序创建和 tom 羊 属性完全相同的 10 只羊. 2 传统方式解决克隆羊问题 1) 思路分析(图 ...

  9. spring框架源码编译

    程序猿小菜一枚,最近从github上面下载下来一份spring各个项目的源码一份,但是弄了很长时间,因为网速和(fanqiang)的速度,希望大家不要再为这种无谓的时间花费时间,简单来说github上 ...

随机推荐

  1. Linux centos 安装 JDK 8

    一.下载JDK 官方下载 # 下载地址 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151. ...

  2. 【硬核摄影2.0】用线性CCD器件制作扫描相机

    本文参考资料:[1] (Strongly Recommend!) Fundamentals and Experiments of Line Scan Camera: http://www.elm-ch ...

  3. dpkg:处理 xxx (--configure)时出错解决办法,也可用于卸载软件出错的情况

    dpkg:处理 xxx (--configure)时出错解决办法今早安装nfs时出现问题,找到该文,备份留用.然后在网上找到了这片文章,按步骤走就解决了,中间会提示自动卸载一下,执行那个命令就好了,我 ...

  4. 源码解析Spring AOP的加载与生效

    本次博主主要进行Spring AOP这里的解析,因为在工作中使用后,却不知道背后的实现原理并在使用的过程中发现了一些认知缺陷,所以决定写这么一篇文章以供大家参考参考,进入正题. 本次博主使用了@Asp ...

  5. 虚拟数字存储表——SQLServer2012可高用

    窗口函数之虚拟数字辅助表 数字辅助表是一个整数序列,可以用它来完成多种不同的查询任务.数字表有很多任务,如生成日期和时间值序列,及分裂值列表.通常,建议在数据库中保存这样一个永久表,并填充尽可能多的数 ...

  6. linux7(centos7)新系统安装后要做的事!

    前言: 初学者在安装linux(centos)系统后,需要对服务器的环境做些简单配置! 怎么联网? 怎么对SSH优化设置? 怎么在众多服务器中识别谁是谁? 常用的小工具推荐等等... ###网络优化设 ...

  7. 20210713考试-2021noip14

    T1 队长快跑 #include<bits/stdc++.h> using namespace std; const int N=1e6+5,INF=0x7fffffff; int n,a ...

  8. 判断输入框中输入的日期格式为yyyy-mm-dd和正确的日期

    判断输入框中输入的日期格式为yyyy-mm-dd和正确的日期   function IsDate(str) { //如果是正确的日期格式返回true,否则返回false var regExp; reg ...

  9. input 只可以输入时分秒

    在html5的time中,只有时.分,没有秒. 例如<input type="time" name="user_date" /> 属性加上 step ...

  10. [第八篇]——Docker 容器使用之Spring Cloud直播商城 b2b2c电子商务技术总结

    Docker 客户端 docker 客户端非常简单 ,我们可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项. xxx@xxx:~# docker 可以通过命令  docke ...