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

Spring DI applicationContext.xml究竟是如何使用xsi:schemaLocation的?,spring,dependency-injection,applicationcontext,Spring,Dependency Injection,Applicationcontext,注意:我提到的测试项目可以通过以下方式下载: <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

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

<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}"/>                          
git克隆

<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}"/>                          
。。和“蚂蚁跑”一起跑

<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}"/>                          
我“理解”XML名称空间名称只是用作不透明标识符,并不打算用作URI()。我还“理解”XML模式位置旨在提供关于模式文档实际位置的提示,作为提示,在实践中没有使用()。考虑到这一点,我一直在通过修改applicationContext.xml来试验一个简单的springdi应用程序(在简单的J2SE设置中使用)。以下是起始版本:

<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}"/>                          
这似乎表明SpringDI运行时使用xsi:schemaLocation中每对中的第二个URL作为某种标识符(由于没有网络访问,所以在其逻辑中进行了硬编码)。因此,我的假设是Spring DI运行时对每个名称空间使用两种标识符:xmlns标识符来唯一标识名称空间(用作不透明字符串)和schemaLocation标识符来唯一标识该名称空间的模式版本(再次用作不透明字符串)。也就是说,schemaLocation实际上被用来(以一种扭曲的方式?因为w3c文档似乎并不打算这样做)对名称空间进行版本化。 此外,在这种情况下,为什么Spring DI运行时不抱怨“p”命名空间缺少schemaLocation。我的思维模式正确吗

以下是发生的情况:

<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}"/>                          
  • XMLSchema允许您为XML名称空间定义别名(简称)。从技术上讲,所有名称空间都由完整URI标识,但这将非常麻烦-因此您可以使用短别名,如
    context
    p
    。还有一个默认名称空间,由
    xmlns
    属性表示

  • <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}"/>                          
    
  • 默认情况下,XML解析器假定命名空间URI也是XSD文件URL位置。通常是这样,但规范并不要求这样做。如果不提供
    schemaLocation
    属性,Spring中的XML解析器也是这样工作的

  • <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}"/>                          
    
  • schemaLocation
    用于从命名空间URI映射到XSD文件物理位置(URL)。当模式名称空间不指向有效的XSD URL时使用它(请参阅)

  • <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}"/>                          
    
  • 最后但并非最不重要的一点是,Spring添加了另一层,将Internet URL转换为类路径上的本地文件。这样,您的应用程序就可以在没有Internet连接的情况下启动(或者当springframework.org网站关闭时)

  • <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}"/>                          
    
如果搜索项目库,您将找到几个名为
spring.schemas
的文件。这些文件包含以下类似的行(摘自
spring context.jar中的文件,我添加了对齐):

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

谢谢,这是一个清晰的模型。如果可以的话,还有两个问题:[1]在我的项目库中只有一个spring.schemas文件(在spring.jar中),而且它似乎不包含您为“xsi”和“p”名称空间描述的映射(尽管我可以找到“context”和根名称空间映射),[2]映射的东西记录在哪里?@MenelaosPerdikeas:不幸的是,我不知道[1]和[2]的答案。