zoukankan      html  css  js  c++  java
  • spring16-----XML命名空间和Spring配置文件中的头

    一. 什么是命名空间

    在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突。类似package的作用。

    这个 XML 文档携带着某个表格中的信息:

    1 <table>
    2    <tr>
    3    <td>Apples</td>
    4    <td>Bananas</td>
    5    </tr>
    6 </table>
    View Code

    这个 XML 文档携带有关桌子的信息(一件家具):

    1 <table>
    2    <name>African Coffee Table</name>
    3    <width>80</width>
    4    <length>120</length>
    5 </table>
    View Code

    假如这两个 XML 文档被一起使用,由于两个文档都包含带有不同内容和定义的 <table> 元素,就会发生命名冲突。

    XML 解析器无法确定如何处理这类冲突。

    1. 使用前缀来避免命名冲突

    此文档带有某个表格中的信息:

    1 <h:table>
    2    <h:tr>
    3    <h:td>Apples</h:td>
    4    <h:td>Bananas</h:td>
    5    </h:tr>
    6 </h:table>
    View Code

    此 XML 文档携带着有关一件家具的信息:

    1 <f:table>
    2    <f:name>African Coffee Table</f:name>
    3    <f:width>80</f:width>
    4    <f:length>120</f:length>
    5 </f:table>
    View Code

    现在,命名冲突不存在了,这是由于两个文档都使用了不同的名称来命名它们的 <table> 元素 (<h:table> 和 <f:table>)。

    通过使用前缀,我们创建了两种不同类型的 <table> 元素。

    2. 使用命名空间(Namespaces)

    这个 XML 文档携带着某个表格中的信息:

    1 <h:table xmlns:h="http://www.w3.org/TR/html4/">
    2    <h:tr>
    3    <h:td>Apples</h:td>
    4    <h:td>Bananas</h:td>
    5    </h:tr>
    6 </h:table>
    View Code

    此 XML 文档携带着有关一件家具的信息:

    1 <f:table xmlns:f="http://www.w3school.com.cn/furniture">
    2    <f:name>African Coffee Table</f:name>
    3    <f:width>80</f:width>
    4    <f:length>120</f:length>
    5 </f:table>
    View Code

    与仅仅使用前缀不同,我们为 <table> 标签添加了一个 xmlns 属性,这样就为前缀赋予了一个与某个命名空间相关联的限定名称。

    3. XML Namespace(xmlns)属性

    XML 命名空间属性被放置于元素的开始标签之中,并使用以下的语法:

    xmlns:namespace-prefix="namespaceURI"    (note:有时候prefix可以省略,直接xmlns="",比如默认的命名空间)

    当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。

    注释:用于标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。不过,很多公司常常会作为指针来使用命名空间指向实际存在的网页,这个网页包含关于命名空间的信息。

    4. XML命名空间的声明

    xmlns:类似于一个保留字,它只用于声明命名空间

    eg:

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

    aop:这里实际上是将前缀“aop”与命名空间"http://www.springframework.org/schema/aop"(这个URI包含关于命名空间的信息)绑定在一起。通常我们会用一个比较简短或约定俗成的名字来作为命名空间的前缀(例如这里的aop),但具体使用什么前缀完全取决于个人.自定义命名空间的前缀是合法的。使用有意义的命名空间前缀增强了XML档的清晰性。所以可以看到我们平时在配置Spring配置文件的时候,前缀名都是aop(切面)、tx(事务)等命名方式。

    5. 重要的配置

    比如spring中的:

    首先xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance 是必须有的。
    xsi:schemaLocation:为指定了用于解析和校验xml的定义文件(xsd)的位置。

    xmlns是xml命名空间,xmlns:xsi是指xml所遵守的标签规范

    6. 指明命名空间的Schema文件地址的用途

    指明命名空间的Schema 文件地址有两个用途:

    1、XML解析器可以获取Schema文件并对文档进行格式化验证;

    2、在开发环境下,Ide(如eclipse)可以引用Schema文件对文档编辑器提供诱导功能,如自动完成提示。

    二. Spring的命名空间

    Spring命名空间有13个,分别如下:

    Spring完整的配置文件命名空间如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:aop="http://www.springframework.org/schema/aop"
     5     xmlns:c="http://www.springframework.org/schema/c"
     6     xmlns:cache="http://www.springframework.org/schema/cache"
     7     xmlns:context="http://www.springframework.org/schema/context"
     8     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     9     xmlns:jee="http://www.springframework.org/schema/jee"
    10     xmlns:lang="http://www.springframework.org/schema/lang"
    11     xmlns:mvc="http://www.springframework.org/schema/mvc"
    12     xmlns:p="http://www.springframework.org/schema/p"
    13     xmlns:task="http://www.springframework.org/schema/task"
    14     xmlns:tx="http://www.springframework.org/schema/tx"
    15     xmlns:util="http://www.springframework.org/schema/util"
    16     xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
    17         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
    18         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    19         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    20         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
    21         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
    22         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
    23         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
    24         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    25         http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
    26         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
    27  
    28  
    29 </beans>
    View Code

    三. Spring找到校验XML的xsd文件

     Spring默认在启动时是要从配置的命名空间的位置加载XSD文件来验证xml文件的,所以如果有的时候断网了,或者一些开源软件切换域名,那么就很容易碰到应用启动不了。

    为了防止这种情况,Spring提供了一种机制,即默认从本地加载XSD文件,当本地没有时才根据实际的URI去联网获得。

    我们打开Spring-aop-4.1.6RELEASE.jar (这是我本地的版本),这个包下有一个META_INF文件夹,其中有两个文件:spring.handlers和spring.schemas。

    spring.handlers

    1
    http://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler

    spring.schemas

    复制代码
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd=org/springframework/aop/config/spring-aop-4.0.xsd
    http://www.springframework.org/schema/aop/spring-aop-4.1.xsd=org/springframework/aop/config/spring-aop-4.1.xsd
    http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-4.1.xsd
    复制代码

     我们看到一个xsd文件对应本地的一个路径,我们打开org/springframework/aop/config/可以看到:

    这就很明显,Spring是把XSD文件放到本地了,再在spring.schemas里做了一个映射,优先从本地里加载XSD文件。

    并且把spring旧版本的XSD文件也全放了。这样可以防止升级了Spring版本,而配置文件里用的还是旧版本的XSD文件,然后断网了,应用启动不了。

    注意我在spring.schemas中标红的最后一行,说明我们在写命名空间值对应的xsd文件位置时,可以不用写版本号,它默认的是本地spring相关版本的对应xsd版本,我这里是4.1。

    在xsi:schemaLocation中这样写:http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

    总结一句话就是:命名空间是为了防止相同标签解析冲突(添加命名空间相当于添加了前缀来隔开),而shema对应的地址(.xsd文件)规定了文件应该遵循的规则(比如标签怎么写等等)。

    参考文献

    https://blog.csdn.net/java_xuetu/article/details/80064019

    http://www.w3school.com.cn/xml/xml_namespaces.asp

    https://www.cnblogs.com/gonjan-blog/p/6637106.html

  • 相关阅读:
    leetcode 对称二叉树
    leetcode 验证二叉搜索树
    蓝桥杯 完美的代价 贪心
    蓝桥杯 字符串对比 模拟
    蓝桥杯 芯片测试 极限找规律
    蓝桥杯 2n皇后问题 深搜
    74. 搜索二维矩阵
    二分 34
    二分 35
    二分 69
  • 原文地址:https://www.cnblogs.com/Hermioner/p/10213570.html
Copyright © 2011-2022 走看看