java Spring 如何从类路径中找到 XSD 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31926712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:22:08  来源:igfitidea点击:

How Spring finds the XSD file from classpath

javaxmlspringxsd

提问by learner

In our spring configuration we put the beans tag like this:

在我们的 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">

Now spring has to find out from my calsspath for the location of the files spring-beans.xsd& spring-context.xsd.

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

I have found some xsd files at this path:

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

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

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

When searching for spring-beans.xsd file I found it some files in this path:

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

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

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

How spring will know where to look for this file as there is no link between the schema location in my beanstag and the actual location of this file. Also I am able to find out files like this spring-beans-<version>.xsdthen how spring will know what is spring-beans.xsdeven when we do not specify any version in <beans>tag.

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

采纳答案by Xstian

Some spring libraries contain some file like META-INF/spring.schemas

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

The properties file called 'spring.schemas' contains a mapping of XML Schema locations (referred to along with the schema declaration in XML files that use the schema as part of the 'xsi:schemaLocation' attribute) to classpath resources. This file is needed to prevent Spring from absolutely having to use a default EntityResolver that requires Internet access to retrieve the schema file. If you specify the mapping in this properties file, Spring will search for the schema on the classpath

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

e.g.

例如

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

spring-beans-xxx-RELEASE.jar 的 spring.schema

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

In few words, above properties allows it to map XSD resource to schemaLocationattribute.

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