XML配置文件的命名空间与Spring配置文件中的头

一直以来,写Spring配置文件,都是把其他配置文件的头拷贝过来,最多改改版本号,也不清楚哪些是需要的,到底是干嘛的。今天整理一下,拒绝再无脑copy。

一、Spring配置文件常见的配置头

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    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
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    
    <!--中间是配置文件部分-->
    
    <bean id="txManager"    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="find*" propagation="NOT_SUPPORTED" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:aspect id="***" ref="***"/>
        <aop:pointcut id="***" expression="****" />
    </aop:config>
    
    <!--中间是配置文件部分-->
    
    </beans>

 重点关注配置文件头部分,就是写在<beans >元素里的部分。是不是感觉到熟悉又陌生?   仔细了解后,不会再那么神秘了。

二、什么是XML命名空间

  XML 命名空间 是由国际化资源标识符 (IRI) 标识的 XML 元素和属性集合;该集合通常称作 XML“词汇”。在XML中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突。举个简单的栗子,命名空间很像 Java 中的包,不同的包下面可以存放相同的类名,只要在引入类时前面加上类的包就可以避免同名类的冲突。

三、XML命名空间的声明与使用

  命名空间被声明为元素的属性。并不一定只在根元素声明命名空间;而是可以在 XML 文档中的任何元素中进行声明。声明的命名空间的范围起始于声明该命名空间的元素,并应用于该元素的所有内容,直到被具有相同前缀名称的其他命名空间声明覆盖,其中,元素内容是指该元素的 <opening-tag> 和 </closing-tag> 之间的内容。

例如:上面的命名空间是在 <beans> </beans>元素中声明的,所有其中声明的命名空间在这两个标签中有效。

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

xmlns:类似于一个保留字,它只用于声明命名空间。换言之,xmlns 用于绑定命名空间,但其本身并不绑定到任何命名空间。

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


配置了前缀后,我们使用命名空间前缀如下:

<aop:config>
<aop:aspect id="***" ref="***"/>
<aop:pointcut id="***" expression="****" />
</aop:config>

这里我们在配置面向切面编程的内容时,使用aop前缀,代表后面的元素(config,aspect等)都是在http://www.springframework.org/schema/aop中定义的。请注意,前缀只用作占位符,并且必须通过可以识别命名空间的 XML 分析器进行解释才能使用绑定到该前缀的实际命名空间。

单个默认命名空间

我们看到,在配置文件中,beans,bean等元素我们是没有使用命名空间前缀的。重复限定一个要在命名空间中使用的元素或属性可能会非常麻烦。
这种情况下,可以声明一个 默认命名空间。无论在任何时候都只能存在一个默认命名空间
声明一个 默认命名空间 意味着,如果 默认命名空间 声明范围内的任何元素未使用前缀显式限定,则该元素将被隐式限定。与带前缀的命名空间一样,
默认命名空间 也可以被覆盖。
默认命名空间声明方式如下:

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

也就是说,在命名空间范围内,不带有前缀的元素都是在这个命名空间内的,例如这里的<beans> <bean>等,因为比较常用所以就让
声明他们的命名空间为默认的啦,不用每次写都带前缀。

四、Spring配置文件配置命名空间

spring 整合了各种工具,并且spring提供了对各种工具的xml scheme 的配置方式,简化了开发。对于各种工具的xml命名空间的引入,我们也应该有一个比较清楚的认识。Spring在启动时是要检验XML文件的。如果xml空间存在命名空间内没有的元素是要报错的。通常情况下,命名空间对应的URI是一个存放XSD的地址,尽管规范没有这么要求。如果没有提供schemaLocation,那么Spring的XML解析器会从命名空间的URI里加载XSD文件。

例如我们可以这样写:

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

 则默认的命名空间就是加载指定的xsd文件。


schemaLocation提供了一个xml 命名空间到对应的XSD(Xml Schema Definition)文件的一个映射,它的值由一个或多个URI引用对组成,
两个URI之间以空白符分隔(空格和换行均可)。第一个URI是定义的 XML命名空间的值,第二个URI给出Schema文档的实际位置,
Schema处理器将从这个位置读取Schema文档,该文档的targetNamespace必须与第一个URI(XML命名空间的值)相匹配。

这里的注意与下面aop的命名空间URI位置对比一下。

在xsi:schemaLocation后面配置的字符串都是成对的,前面的是命名空间的URI,后面是xsd文件的URI;
比如我们给出的例子(注意这里已经用了一个命名前缀:xsi,这个xsi是在xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 声明的):

    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
           http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    

 这里命名空间aop的值是“http://www.springframework.org/schema/aop”,它对应的xsd文件的位置为“http://www.springframework.org/schema/aop/spring-aop-3.0.xsd”

我们打开http://www.springframework.org/schema/aop/spring-aop-3.0.xsd,可以看到xsd文件中targetNamespace的值和命名空间的值一样。如下:

 五、Spring找到校验XML的xsd文件

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

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

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

spring.handlers

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

 六、总结

 对于spring配置文件的头,已经有了一个比较清楚的认识。在以后写配置文件的时候不会再一股脑得全拷贝过来。首先看看需要哪些功能,再导入相应的命名空间。

最好是在写命名空间值对应的xsd文件位置的时候,不加版本号,让spring加载本地版本对应的xsd文件。

当然,也可以让spring跳过对XML的校验,具体方法:eclipse中,windows右键 ,去掉下图红色箭头处的勾,就可以对本工作空间取消验证xml文件: 

或者针对某个单独的xml文件右键,validate也行。

针对已经验证过的文件,而且也没做修改。我们可以取消对其自动验证。对于新建的配置文件,我觉得最好还是不要取消验证的好,毕竟我们在配置文件的时候,有验证会让我们今早发现错误。

补充,stackoverflow下的一个问题解答,很好的说明了 XML schema 命名空间如何与对应的class关联起来的。也就是spring.handlers文件的作用。

http://stackoverflow.com/questions/11174286/spring-xml-namespaces-how-do-i-find-what-are-the-implementing-classes-behind-t

                  --总觉得自己讲得不太清楚,还需要慢慢练习,多写博客,锻炼表达写作能力,文中有表达不清或有误的地方欢迎留言指正讨论。

---------------------------------

             2017-3-29  Gonjan

原文地址:https://www.cnblogs.com/gonjan-blog/p/6637106.html