Spring DI applicationContext.xml xsi:schemaLocation 究竟是如何使用的? [英] Spring DI applicationContext.xml how exactly is xsi:schemaLocation used?

查看:26
本文介绍了Spring DI applicationContext.xml xsi:schemaLocation 究竟是如何使用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Note: the test project I'm mentioning can be downloaded with:

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

.. 并用 'ant run' 运行.

.. and run with 'ant run'.

我理解"XML 命名空间名称仅用作不透明标识符,并不打算用作 URI (维基百科).我还理解"XML 模式位置旨在提供有关模式文档实际位置的提示,作为提示,在实践中并未使用(w3.org).考虑到这一点,我一直在通过修改 applicationContext.xml 来试验一个简单的 Spring DI 应用程序(在简单的 J2SE 设置中使用).这是起始版本:

I 'understand' that XML namespace names are just used as opaque identifiers and not meant to be used as URIs (wikipedia). I also 'understand' that the XML schema locations are meant to provide hints as to the actual location of schema documents and, being hints, not used in practice (w3.org). With that in mind I 've been experimenting with a simple Spring DI application (used in a simple J2SE setting) by modifying the applicationContext.xml. Here is the starting version:

<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"                                           
      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">                    

<context:component-scan base-package="atm"/>                                                                       
<context:property-placeholder location="classpath:META-INF/spring/atm.properties"/>                                

<bean id="soapTransport_" class="atm.SoapATMTransport" p:retries="${transport.retries}"/>                          

当我执行 'sudo ifconfig eth0 down' 时,项目运行完美,这与运行时一致,无需费心从 schemaLocations 获取任何内容.但是,当我通过向每对中的第二个 URL 添加一个简单的下划线来修改 schemaLocations 时,我收到了以下投诉:

When I did a 'sudo ifconfig eth0 down' the project ran perfectly which is consistent with the runtime not bothering to fetch anything from the schemaLocations. However, when I mangled the schemaLocations by adding a simple underscore to the second URL in each pair I received the following complaint:

 [java] org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from class path resource [META-INF/spring/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 100; cvc-elt.1: Cannot find the declaration of element '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 运行时在 xsi:schemaLocation 处使用每对中的第二个 URL 作为某种标识符(在其逻辑中进行硬编码,因为没有网络访问).所以我的假设是 Spring DI 运行时为每个命名空间使用两种标识符:xmlns 标识符来唯一标识命名空间(用作不透明字符串)和 schemaLocation 标识符以唯一标识该命名空间的架构版本(再次用作不透明字符串).IE.schemaLocation 实际用于(以扭曲的方式?因为这似乎不是 w3c 文档的意图)来版本命名空间.此外,在这种情况下,为什么 Spring DI 运行时不会抱怨p"命名空间缺少 schemaLocation.我的心智模型正确吗?

Which seems to suggest that Spring DI runtime uses the second URL in each pair at the xsi:schemaLocation as some kind of identifier (hardcoded in its logic since there is no network access). So my assumption would be that the Spring DI runtime uses two kind of identifiers for each namespace: the xmlns identifier to uniquely identify the namespace (used as an opaque string) and the schemaLocation identifier to uniquely identify the schema version for that namespace (again used as an opaque string). I.e. the schemaLocation is actually used (in a contorted way ? as that doesn't seem to be the intention of the w3c documents) to version the namespace. Moreover, in such a case, why doesn't the Spring DI runtime complain about the lack of a schemaLocation for the "p" namespace. Is my mental model correct?

推荐答案

这是发生了什么:

  • XML 模式允许您定义 XML 命名空间的别名(简称).从技术上讲,所有命名空间都由完整 URI 标识,但这会非常麻烦 - 因此您可以使用短别名,例如 contextp.还有一个由 xmlns 属性

  • XML schema allows you to define aliases (short names) to XML namespaces. Technically all namespaces are identified by full URI but that would be very cumbersome - so you can use short aliases like context and p. There is also a default namespace denoted by xmlns attribute

默认情况下,XML 解析器假定命名空间 URI 也是 XSD 文件 URL 位置.这通常是这种情况,但规范没有要求.如果您不提供 schemaLocation 属性,这也是 Spring 中 XML 解析器的工作方式.

By default XML parsers assume namespace URI is also a XSD file URL location. This is often the case, but is not required by the spec. This is also how XML parser in Spring works if you don't provide schemaLocation attribute.

schemaLocation 用于从命名空间 URI 映射到 XSD 文件物理位置 (URL).当架构命名空间指向有效的 XSD URL 时使用它(请参阅 MSDN onschemaLocation).

schemaLocation is used to map from namespace URI to XSD file physical location (URL). It is used when schema namespace does not point to valid XSD URL (see MSDN on schemaLocation).

最后但并非最不重要的一点是,Spring 添加了另一层,将 Internet URL 转换为 CLASSPATH 上的本地文件.这样您的应用程序就可以在没有 Internet 连接的情况下启动(或在 springframework.org 站点关闭时).

Last but not least, Spring adds yet another layer that translates Internet URLs to local files on CLASSPATH. This way your application can start without Internet connection (or when springframework.org site is down).

如果您搜索您的项目库,您会发现多个名为 spring.schemas 的文件.这些文件包含类似于下面的行(从 spring-context.jar 中找到的文件中提取,我添加了对齐):

If you search your project libraries you'll find several files named spring.schemas. These files contain lines similar to below (extract from the file found in spring-context.jar, I added alignment):

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

这篇关于Spring DI applicationContext.xml xsi:schemaLocation 究竟是如何使用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆