Spring DI applicationContext.xml中的xsi:schemaLocation是如何使用的?

11 浏览
0 Comments

Spring DI applicationContext.xml中的xsi:schemaLocation是如何使用的?

注意:我提到的测试项目可以通过以下方式下载:

git clone https://github.com/mperdikeas/so-spring-di-appcontext-schemalocation.git

.. 并使用 'ant run' 运行。

我‘了解’ XML 命名空间名称只是用作不透明标识符,而不是用作 URI(维基百科)我也‘了解’ XML 模式位置旨在提供有关模式文档实际位置的提示,并且作为提示,实际上并没有在实践中使用(w3.org)。考虑到这一点,我通过修改 applicationContext.xml,对一个简单的 Spring DI 应用程序(在简单的 J2SE 环境中使用)进行了实验。这是起始版本:

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

xmlns:context = "http://www.springframework.org/schema/context"

xmlns:p = "http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd">

当我执行 'sudo ifconfig eth0 down' 时,项目运行得很完美,这与运行时不会打扰从 schemaLocations 获取任何内容是一致的。然而,当我在每对中的第二个 URL 中添加一个简单的下划线来修改 schemaLocations 时,我收到了以下投诉:

 [java] org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: 来自类路径资源 [META-INF/spring/applicationContext.xml] 的 XML 文档中的第 10 行无效; 嵌套异常是 org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 100; cvc-elt.1: 无法找到元素 'beans' 的声明。
 [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:194)
 [java]     at org.apache.tools.ant.taskdefs.Java.run(Java.java:771)
 [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:221)
 [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:135)
 [java]     at org.apache.tools.ant.taskdefs.Java.execute(Java.java:108)
 [java]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 [java]     at java.lang.reflect.Method.invoke(Method.java:601)
 [java]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
 [java]     at org.apache.tools.ant.Task.perform(Task.java:348)
 [java]     at org.apache.tools.ant.Target.execute(Target.java:390)
 [java]     at org.apache.tools.ant.Target.performTasks(Target.java:411)
 [java]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
 [java]     at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
 [java]     at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
 [java]     at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
 [java]     at org.apache.tools.ant.Main.runBuild(Main.java:809)
 [java]     at org.apache.tools.ant.Main.startAnt(Main.java:217)
 [java]     at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
 [java]     at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

这似乎表明 Spring DI 运行时使用每对中的第二个 URL 作为 xsi:schemaLocation 中的某种标识符(在其逻辑中硬编码,因为没有网络访问)。因此,我的假设是 Spring DI 运行时对每个命名空间使用两种类型的标识符:xmlns 标识符用于唯一标识命名空间(作为不透明字符串使用),而 schemaLocation 标识符用于唯一标识该命名空间的模式版本(同样作为不透明字符串使用)。也就是说,schemaLocation 实际上是以一种扭曲的方式使用的(这似乎不是 w3c 文档的意图),用于对命名空间进行版本控制。

此外,在这种情况下,为什么 Spring DI 运行时不会抱怨 "

" 命名空间缺少 schemaLocation。我的心理模型正确吗?

0
0 Comments

在Spring的应用程序上下文文件(applicationContext.xml)中,使用xsi:schemaLocation来指定XML命名空间和对应的XSD文件的物理位置。这个问题的出现是因为XML解析器默认会将命名空间URI作为XSD文件的URL地址,但是这并不是规范要求的。当命名空间URI不是有效的XSD URL时,就需要使用schemaLocation来映射命名空间URI和XSD文件的物理位置。

Spring还添加了另一个层次,将互联网上的URL转换为CLASSPATH上的本地文件。这样,即使应用程序没有互联网连接或者springframework.org网站不可用,应用程序仍然可以正常启动。

在项目的库中搜索,你会找到几个名为spring.schemas的文件。这些文件包含类似于以下内容的行(从spring-context.jar文件中提取的示例):

http\://www.springframework.org/schema/context/spring-context.xsd= org/springframework/context/config/spring-context-3.1.xsd

http\://www.springframework.org/schema/jee/spring-jee.xsd= org/springframework/ejb/config/spring-jee-3.1.xsd

http\://www.springframework.org/schema/lang/spring-lang.xsd= org/springframework/scripting/config/spring-lang-3.1.xsd

http\://www.springframework.org/schema/cache/spring-cache.xsd= org/springframework/cache/config/spring-cache-3.1.xsd

这些文件定义了命名空间URI和对应XSD文件的映射关系。

至于你提到的两个额外的问题:[1]在项目的库中只有一个spring.schemas文件(在spring.jar中),它似乎没有包含你描述的'xsi'和'p'命名空间的映射(尽管可以找到'context'和根命名空间的映射),[2]这个映射的文档在哪里?

不幸的是,我不知道这两个问题的答案。

0