zoukankan      html  css  js  c++  java
  • Spring的xml文件详解

    spring的xml配置文件头:

    <?xml version="1.0" encoding="UTF-8"?>
    <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-3.0.xsd
                            http://www.springframework.org/schema/context                                                                             
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
                           
                            ...<!--中间xml文件部分-->.....
        </beans>
    

    一直在复制黏贴,但是不知道作用是什么,不理解的话常出错。。。

    xmlns和命名空间

    首先,介绍一下xmlns的作用,如下所示,一个 xml 文档中如果包含如下两种定义不同, 但是名称相同的元素, xml 解析器是无法解析的, 因为它不能确定当你调用document.getElementsByTagName("book") 时应该返回哪个元素。

    <!-- 这里的 table 元素描述的是一个表格-->
    <table>
       <tr>
       <td>Apples</td>
       <td>Bananas</td>
       </tr>
    </table>
    <!-- 这里的 table 元素描述的是一个家居桌子-->
    <table>
       <name>African Coffee Table</name>
       <width>80</width>
       <length>120</length>
    </table>
    

    这时候可以通过在名称增加前缀解决这个问题

    <!-- 这里的 table 元素描述的是一个表格-->
    <h:table>  <!--添加了前缀 h -->
       <h:tr>
       <h:td>Apples</h:td>
       <h:td>Bananas</h:td>
       </h:tr>
    </h:table>
    <!-- 这里的 table 元素描述的是一个表格-->
    <f:table> <!--添加了前缀 f -->
       <f:name>African Coffee Table</f:name>
       <f:width>80</f:width>
       <f:length>120</f:length>
    </f:table>
    

    由此,引入一个概念 命名空间,通过增加前缀表示不同的那是不同命名空间下的table,从而解决了矛盾,但是不同的人都有自己创建的不同的命名空间来描述同样的东西,不利于xml文件信息的解析,比如说,同样都是水果,可以从颜色和香味不同角度来定义成如下两种形式:

    <!--按照水果香味来定义-->
    <perfume:fruit>
          <name>....</name>
          <perfume>.....</perfume>
    </perfume:fruit>
    
    <!--按照水果颜色来定义-->
    <color:fruit>
          <name>....</name>
          <color>....</color>
    </color:fruit>
    

    为此,w3c(万维网联盟)对于一些类型,定义了对应的命名空间和这些类型的标准,xml解释器碰到这些类型的时候就会通过这些标准去解析这类型的标签,为了确保命名空间的唯一,所以不同的命名空间的通常使用URL作为被识别的id,如下例子:

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

    这句话的作用是当前引入了一个叫做xsi的命名空间,xsi可以在接下来要使用该命名空间时所使用的,如下:

    <xsi:schemaLocation="...... ......">
    

    http://www.w3.org/2001/XMLSchema-instance这个很长的字符串,则是xsi这个名称空间被xml解释器内部所识别的时候所真正使用的id,但也本身只是被当做一个字符串名字去处理,xml解释器根据这个id去获取它对应的标准,从而知道这个命名空间定义有什么样的标签(xml解释器自带有一些通用的命名空间的标准),这个字符串虽然看起来是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"
    

    所以,spring配置文件中这三句话分别表示,引入了三个命名空间。
    其中第一个xmlns后面没有空间名的,表示引入了一个默认的名称空间,下文中不使用命名空间前缀的都默认使用这个命名空间,这个默认的命名空间,其真正的id是"http://www.springframework.org/schema/beans"。
    引入的第二个命名空间叫做xsi,其真正的id是"http://www.w3.org/2001/XMLSchema-instance"
    引入的第三个命名空间叫做context,其真正的id是"http://www.springframework.org/schema/context"

    xsi:schemaLocation

        xsi:schemaLocation="http://www.springframework.org/schema/beans            
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/context                                                                             
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    

    在最后可以看到xsi:schemaLocation,这句话的意思表示使用命名空间xsi下的schemaLocatioin,设置了它对应的值为后面很多很多的URL,schemaLocation中存储的值每两个为一组,第一个代表命名空间,第二个代表该命名空间的标准的文件位置,如下所示,这句话就是说明命名空间http://www.springframework.org/schema/beans的标准文件是http://www.springframework.org/schema/beans/spring-beans-3.0.xsd*

        xsi:schemaLocation="http://www.springframework.org/schema/beans            
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    

    因为xml解释器不一定含有所有命名空间的标准,通过这样设置就可以告诉xml解释器不同命名空间的对应的标准是什么了,而这也是xsi这个命名空间的作用,要用到其schemaLocation。

    最后,对应一般的xml解释器的工作流程中,xml解释器识别到有“http://www.w3.org/2001/XMLSchema-instance"这个通用的名称空间后,明白知道要引入一些不同命名空间,就会从其schemaLocation中获取不同命名空间和其对应的标准。

  • 相关阅读:
    好还是坏:人工智能二分类问题
    神经网络手写数字识别
    TensorFlow or PyTorch
    什么是深度学习
    五个常见 AI 开发库
    AI——第四次工业革命
    NodeJs获取不到POST参数
    Android权限
    【nodejs学习】3.进程管理及异步编程
    每日一题
  • 原文地址:https://www.cnblogs.com/zbm-gg/p/13399379.html
Copyright © 2011-2022 走看看