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

lea*_*ner 10 java xml spring xsd

在我们的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">
Run Code Online (Sandbox Code Playgroud)

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

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

弹簧上下文4.1.5.RELEASE.jar /组织/ springframework的/上下文/配置

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
Run Code Online (Sandbox Code Playgroud)

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

弹簧豆-4.1.5.RELEASE.jar /组织/ springframework的/豆/工厂/ 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
Run Code Online (Sandbox Code Playgroud)

spring如何知道在哪里查找此文件,因为我的beans标记中的模式位置与此文件的实际位置之间没有链接.此外,我能够找到这样的文件spring-beans-<version>.xsd然后spring-beans.xsd当我们没有在<beans>标签中指定任何版本时,Spring将如何知道甚至是什么.

Xst*_*ian 10

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

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

例如

Spring.schemas of spring-beans-xxx-RELEASE.jar

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
Run Code Online (Sandbox Code Playgroud)

简而言之,上面的属性允许它将XSD资源映射到schemaLocation属性.