【问题标题】:How Spring finds the XSD file from classpathSpring 如何从类路径中找到 XSD 文件
【发布时间】:2015-11-02 18:57:19
【问题描述】:

在我们的 spring 配置中,我们这样放置 beans 标签:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

现在 spring 必须从我的 calsspath 中找出文件 spring-beans.xsdspring-context.xsd 的位置。

我在这个路径找到了一些 xsd 文件:

spring-context-4.1.5.RELEASE.jar/org/springframework/context/config

spring-context-2.5.xsd
spring-context-3.0.xsd
spring-context-3.1.xsd
spring-context-3.2.xsd
spring-context-4.0.xsd
spring-context-4.1.xsd

在搜索 spring-beans.xsd 文件时,我在此路径中找到了一些文件:

spring-beans-4.1.5.RELEASE.jar/org/springframework/beans/factory/xml

spring-beans-2.5.xsd
spring-beans-3.0.xsd
spring-beans-3.1.xsd
spring-beans-3.2.xsd
spring-beans-4.0.xsd
spring-beans-4.1.xsd

spring 将如何知道在哪里查找此文件,因为我的 beans 标记中的架构位置与此文件的实际位置之间没有链接。我也能找到像spring-beans-&lt;version&gt;.xsd 这样的文件,那么即使我们没有在&lt;beans&gt; 标签中指定任何版本,spring 也将知道spring-beans.xsd 是什么。

【问题讨论】:

    标签: java xml spring xsd


    【解决方案1】:

    一些 spring 库包含一些文件,如 META-INF/spring.schemas

    名为“spring.schemas”的属性文件包含 XML 的映射 架构位置(与 XML 中的架构声明一起引用) 将架构用作“xsi:schemaLocation”一部分的文件 属性)到类路径资源。需要这个文件来防止 从绝对必须使用默认的 EntityResolver 中跳出来 需要 Internet 访问才能检索架构文件。如果您指定 此属性文件中的映射,Spring 将搜索架构 在类路径上

    例如

    spring-beans-x.x.x-RELEASE.jar 的spring.schemas

    http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
    http\://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd
    http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
    http\://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
    http\://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd
    http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd
    

    简而言之,以上属性允许它将 XSD 资源映射到 schemaLocation 属性。

    更多详情,另见Need understanding of spring.handlers and spring.schemas

    【讨论】:

    • 它是如何工作的?指示映射位于 spring.schemas 中的代码在哪里
    猜你喜欢
    • 1970-01-01
    • 2012-11-01
    • 2015-01-24
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 2016-06-15
    相关资源
    最近更新 更多