Spring5源码(16)-Spring将Xml文件解析为Document对象

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: Spring5源码(16)-Spring将Xml文件解析为Document对象


上一节分析了XmlBeanDefinitionReader以及系统环境的初始化,本小节分析Spring解析xml的过程中的将Xml文件解析为Document对象。

先来回顾一下Java解析xml的方式。包括DOM解析、SAX解析XML、JDOM解析XML、DOM4J解析XML等,每种解析方式各有优缺点。Spring使用的是第一种解析方式DOM解析,先通过一个例子来看一下Java是如何将xml文件解析为Document对象的。这将有助于接下来对Spring源码的分析。

1. Java DOM解析xml文件
  • DOM解析

@Test
public void test14() throws ParserConfigurationException, IOException, SAXException {
    // 解析xml文件
    // 1、获取InputStream输入流
    InputStream in = new ClassPathResource("v2/day01.xml").getInputStream();
    // 2、获取DocumentBuilderFactory实例
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // 3、获取DocumentBuilder实例
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    // 4、将docBuilder转换为Document
    Document doc = docBuilder.parse(in);
    // 5、获取节点并循环输出节点值
    Element element = doc.getDocumentElement();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        //System.out.println(node.getNodeName());
        NamedNodeMap attributes = node.getAttributes();
        if (null != attributes) {
            System.out.println(attributes.getNamedItem("id"));
            System.out.println(attributes.getNamedItem("class"));
        }
    }
}
  • 输出

========测试方法开始=======
id="dog1"
class="com.lyc.cn.v2.day01.Dog"
id="dog2"
class="com.lyc.cn.v2.day01.Dog"
id="dog3"
class="com.lyc.cn.v2.day01.DogStaticFactory"
id="dogFactory"
class="com.lyc.cn.v2.day01.DogFactory"
id="dog4"
null
id="outer"
class="com.lyc.cn.v2.day01.inner.Outer"
id="father"
class="com.lyc.cn.v2.day01.parent.Father"
id="sun"
class="com.lyc.cn.v2.day01.parent.Sun"
id="cat"
class="com.lyc.cn.v2.day01.collection.Cat"
id="car"
class="com.lyc.cn.v2.day01.method.lookupMethod.Car"
id="taxi"
class="com.lyc.cn.v2.day01.method.lookupMethod.Taxi"
id="dogReplaceMethod"
class="com.lyc.cn.v2.day01.method.replaceMethod.ReplaceDog"
id="originalDogReplaceMethod"
class="com.lyc.cn.v2.day01.method.replaceMethod.OriginalDog"
id="student"
class="com.lyc.cn.v2.day01.factoryBean.StudentFactoryBean"
id="furniture"
class="com.lyc.cn.v2.day01.factoryBean.FurnitureFactoryBean"
id="myLifeCycleBean"
class="com.lyc.cn.v2.day01.lifecycle.LifeCycleBean"
id="myBeanPostProcessor"
class="com.lyc.cn.v2.day01.lifecycle.LifeCycleBeanPostProcessor"
id="dog"
class="com.lyc.cn.v2.day01.Dog"
id="myBeanFactoryPostProcessor"
class="com.lyc.cn.v2.day01.lifecycle.MyBeanFactoryPostProcessor"
========测试方法结束=======

非常简单,不再做过的分析。

2. Spring将xml转换为Document对象分析

打开XmlBeanFactory类

/**
 * 通过指定Resource对象和父BeanFactory创建XmlBeanFactory实例
 * Create a new XmlBeanFactory with the given input stream,
 * which must be parsable using DOM.
 * @param resource          the XML resource to load bean definitions from
 * @param parentBeanFactory parent bean factory
 * @throws BeansException in case of loading or parsing errors
 */
public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
    // 依次向上实例化父类构造器
    super(parentBeanFactory);
    // 解析xml配置文件,将其转换为IoC容器的内部表示
    this.reader.loadBeanDefinitions(resource);
}

this.reader.loadBeanDefinitions(resource); 该代码的作用就是解析xml配置文件,将其转换为IoC容器的内部表示。我们先分析其第一步操作:解析xml配置文件。

跟踪代码,依次打开

  • 方法入口

/**
 * 加载BeanDefinition
 * Load bean definitions from the specified XML file.
 * @param resource the resource descriptor for the XML file
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
    return loadBeanDefinitions(new EncodedResource(resource));
}
  • 获取InputStream对象

/**
 * 加载BeanDefinition
 * Load bean definitions from the specified XML file.
 * @param encodedResource the resource descriptor for the XML file,
 * allowing to specify an encoding to use for parsing the file
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
    // 1、使用ThreadLocal防止资源文件循环加载
    Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
    if (currentResources == null) {
        currentResources = new HashSet<>(4);
        this.resourcesCurrentlyBeingLoaded.set(currentResources);
    }
    if (!currentResources.add(encodedResource)) {
        throw new BeanDefinitionStoreException("Detected cyclic loading of " + encodedResource + " - check your import definitions!");
    }
    try {
        // 2、加载BeanDefinition
        InputStream inputStream = encodedResource.getResource().getInputStream();
        try {
            InputSource inputSource = new InputSource(inputStream);
            if (encodedResource.getEncoding() != null) {
                inputSource.setEncoding(encodedResource.getEncoding());
            }
            return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
        }
        finally {
            inputStream.close();
        }
    }
    catch (IOException ex) {
        throw new BeanDefinitionStoreException("IOException parsing XML document from " + encodedResource.getResource(), ex);
    }
    finally {
        currentResources.remove(encodedResource);
        if (currentResources.isEmpty()) {
            this.resourcesCurrentlyBeingLoaded.remove();
        }
    }
}
  • 将xml转换为Document对象并执行BeanDefinition注册

/**
 * 真正开始执行BeanDefinition的注册
 * Actually load bean definitions from the specified XML file.
 * @param inputSource the SAX InputSource to read from
 * @param resource the resource descriptor for the XML file
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 * @see #doLoadDocument
 * @see #registerBeanDefinitions
 */
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException {
    try {
        // 资源文件解析为Document对象
        Document doc = doLoadDocument(inputSource, resource);
        // 注册BeanDefinitions
        return registerBeanDefinitions(doc, resource);
    }
    catch (BeanDefinitionStoreException ex) {
        throw ex;
    }
    catch (SAXParseException ex) {
        throw new XmlBeanDefinitionStoreException(resource.getDescription(),"Line "
                + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);
    }
    catch (SAXException ex) {
        throw new XmlBeanDefinitionStoreException(resource.getDescription(),"XML document from " + resource + " is invalid", ex);
    }
    catch (ParserConfigurationException ex) {
        throw new BeanDefinitionStoreException(resource.getDescription(),"Parser configuration exception parsing XML from " + resource, ex);
    }
    catch (IOException ex) {
        throw new BeanDefinitionStoreException(resource.getDescription(),"IOException parsing XML document from " + resource, ex);
    }
    catch (Throwable ex) {
        throw new BeanDefinitionStoreException(resource.getDescription(),"Unexpected exception parsing XML document from " + resource, ex);
    }
}

通过上面代码的分析,已经接触到了将xml文件转换为Document的核心Document doc = doLoadDocument(inputSource, resource);,其实并没有我们想象中那么神秘,跟我们之前分析的DOM解析是一样的。但是其中有一些细节还是值得我们去分析的。

  • 执行转换

@Override
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver,
        ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
    // 1、创建DocumentBuilderFactory对象
    DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
    // 2、创建DocumentBuilder对象
    DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
    // 3、将inputSource解析为Document对象
    return builder.parse(inputSource);
}

转换过程一共分为了三步,这与DOM解析的流程差不多,来具体分析一下其中的一些细节。

1.创建DocumentBuilderFactory对象

protected DocumentBuilderFactory createDocumentBuilderFactory(int validationMode, boolean namespaceAware)
            throws ParserConfigurationException {
    // 1、获取DocumentBuilderFactory实例
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(namespaceAware);
    // 2、如果开启xml验证的话,则验证xml
    if (validationMode != XmlValidationModeDetector.VALIDATION_NONE) {
        factory.setValidating(true);
        // 如果xml验证模式为XSD则需要强制指定由此代码生成的解析器将提供对XML名称空间的支持
        if (validationMode == XmlValidationModeDetector.VALIDATION_XSD) {
            // Enforce namespace aware for XSD...
            factory.setNamespaceAware(true);
            try {
                factory.setAttribute(SCHEMA_LANGUAGE_ATTRIBUTE, XSD_SCHEMA_LANGUAGE);
            }
            catch (IllegalArgumentException ex) {
                ParserConfigurationException pcex = new ParserConfigurationException(
                        "Unable to validate using XSD: Your JAXP provider [" + factory +
                        "] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? " +
                        "Upgrade to Apache Xerces (or Java 1.5) for full XSD support.");
                pcex.initCause(ex);
                throw pcex;
            }
        }
    }
    return factory;
}
  • 2.创建DocumentBuilder对象

protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory,
            @Nullable EntityResolver entityResolver, @Nullable ErrorHandler errorHandler)
            throws ParserConfigurationException {
    // 1、创建DocumentBuilder对象
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    // 2、尝试设置entityResolver
    if (entityResolver != null) {
        docBuilder.setEntityResolver(entityResolver);
    }
    // 3、尝试设置errorHandler
    if (errorHandler != null) {
        docBuilder.setErrorHandler(errorHandler);
    }
    return docBuilder;
}

这里有一个EntityResolver类,该类的作用是避免从网络上寻找DTD声明。至于转换方法本节不在分析,因为涉及到了jdk的源码,且不是我们分析的重点。

总之Spring将Xml文件解析为Document对象的过程就是使用了Java的DOM解析,只不过在解析之上做了一些额外的操作,例如防止文件重复加载、xml验证模式、

设置EntityResolver、设置errorHandler等等。





目录
相关文章
|
1月前
|
存储 Java 文件存储
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— logback.xml 配置文件解析
本文解析了 `logback.xml` 配置文件的详细内容,包括日志输出格式、存储路径、控制台输出及日志级别等关键配置。通过定义 `LOG_PATTERN` 和 `FILE_PATH`,设置日志格式与存储路径;利用 `&lt;appender&gt;` 节点配置控制台和文件输出,支持日志滚动策略(如文件大小限制和保存时长);最后通过 `&lt;logger&gt;` 和 `&lt;root&gt;` 定义日志级别与输出方式。此配置适用于精细化管理日志输出,满足不同场景需求。
138 1
|
1月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
57 0
|
1月前
|
算法 测试技术 C语言
深入理解HTTP/2:nghttp2库源码解析及客户端实现示例
通过解析nghttp2库的源码和实现一个简单的HTTP/2客户端示例,本文详细介绍了HTTP/2的关键特性和nghttp2的核心实现。了解这些内容可以帮助开发者更好地理解HTTP/2协议,提高Web应用的性能和用户体验。对于实际开发中的应用,可以根据需要进一步优化和扩展代码,以满足具体需求。
151 29
|
1月前
|
前端开发 数据安全/隐私保护 CDN
二次元聚合短视频解析去水印系统源码
二次元聚合短视频解析去水印系统源码
56 3
|
1月前
|
JavaScript 算法 前端开发
JS数组操作方法全景图,全网最全构建完整知识网络!js数组操作方法全集(实现筛选转换、随机排序洗牌算法、复杂数据处理统计等情景详解,附大量源码和易错点解析)
这些方法提供了对数组的全面操作,包括搜索、遍历、转换和聚合等。通过分为原地操作方法、非原地操作方法和其他方法便于您理解和记忆,并熟悉他们各自的使用方法与使用范围。详细的案例与进阶使用,方便您理解数组操作的底层原理。链式调用的几个案例,让您玩转数组操作。 只有锻炼思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
|
1月前
|
负载均衡 JavaScript 前端开发
分片上传技术全解析:原理、优势与应用(含简单实现源码)
分片上传通过将大文件分割成多个小的片段或块,然后并行或顺序地上传这些片段,从而提高上传效率和可靠性,特别适用于大文件的上传场景,尤其是在网络环境不佳时,分片上传能有效提高上传体验。 博客不应该只有代码和解决方案,重点应该在于给出解决方案的同时分享思维模式,只有思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
|
1月前
|
存储 前端开发 JavaScript
在线教育网课系统源码开发指南:功能设计与技术实现深度解析
在线教育网课系统是近年来发展迅猛的教育形式的核心载体,具备用户管理、课程管理、教学互动、学习评估等功能。本文从功能和技术两方面解析其源码开发,涵盖前端(HTML5、CSS3、JavaScript等)、后端(Java、Python等)、流媒体及云计算技术,并强调安全性、稳定性和用户体验的重要性。
|
6月前
|
XML 前端开发 Java
讲解SSM的xml文件
本文详细介绍了SSM框架中的xml配置文件,包括springMVC.xml和applicationContext.xml,涉及组件扫描、数据源配置、事务管理、MyBatis集成以及Spring MVC的视图解析器配置。
151 1
|
8月前
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
5月前
|
XML Android开发 数据格式
Eclipse 创建 XML 文件
Eclipse 创建 XML 文件
87 2

热门文章

最新文章

推荐镜像

更多