Spring Bean配置文件——Xml的命名空间

下面是一份非常常见的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"
    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>

XML命名空间的声明与使用

命名空间被声明为元素的属性。并不一定只在根元素声明命名空间;而是可以在 XML 文档中的任何元素中进行声明。声明的命名空间的范围起始于声明该命名空间的元素,并应用于该元素的所有内容,直到被具有相同前缀名称的其他命名空间声明覆盖,其中,元素内容是指该元素的 和 之间的内容。
例如:上面的命名空间是在 元素中声明的,所有其中声明的命名空间在这两个标签中有效。

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,aspect等)都是在http://www.springframework.org/schema/aop 中定义的。请注意,前缀只用作占位符,并且必须通过可以识别命名空间的 XML 分析器进行解释才能使用绑定到该前缀的实际命名空间。

Spring配置文件配置命名空间

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