SpringXML配置文件中的头部定义简析

来源:互联网 发布:计算机动画算法与技术 编辑:程序博客网 时间:2024/06/04 19:39

常见的spring配置文件

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"     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-4.0.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    <context:annotation-config />      <context:component-scan base-package="......">        ......    </context:component-scan>    <mvc:annotation-driven>        <mvc:message-converters register-defaults="true">              <bean class="org.springframework.http.converter.StringHttpMessageConverter">                   ......            </bean>          </mvc:message-converters>    </mvc:annotation-driven>    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">          ......    </bean> </beans>

通常我们看到的spring 配置文件基本都是这样。

元素的xmlns属性

xmlns是”XML Namespace”,也就是命名空间的简写。做为元素的属性,它可以:

  1. 在文档中定义一个或多个可供选择的命名空间。
  2. 可以放置在文档内任何元素的开始标签中。
  3. 其属性值类似于一个URI,它定义了一个命名空间,此命名空间用于该属性所在元素内的所有内容。

例如,对于spring的配置文件,如果不在文档开头如此定义的话,还可以这样写成这样:

<annotation-config xmlns="http://www.springframework.org/schema/context"/><component-scan xmlns="http://www.springframework.org/schema/context" base-apckage="......">.......</component-scan><annotation-driven xmlns="http://www.springframework.org/schema/mvc">    <message-converters xmlns="ttp://www.springframework.org/schema/mvc" register-defaults="true">        ......    </message-converters></annotation-driven>

但是像这样,为每个元素都定义xmlns的话,整个文档会十分冗长;而在文档开头处定义有前缀的命名空间,会使得文档简单清晰很多,减少文档的冗余。
定义前缀的语法很简单:

xmlns:namespace-prefix="namespaceURI"

其中namespace-prefix为自定义前缀,必须在当前XML文档中唯一;namespaceURI是这个前缀对应的XML Namespace的定义。 如:

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

定义了一个”http://www.springframwork.org/schema/context“的Namespace,并将其和前缀context绑定。

前缀名称可以任意设定,设置为”ctx”,”c”,”aaa”,”bbb”都是可以的。设定前缀之后就可以在整个XML文档中使用这个前缀对文档进行简化。

需要注意的是,使用简称前缀的话,不但元素的开始标签要加上相应的前缀,结束标签也要加上相应的前缀。

<context:annotation-config /><context:component-scan>......</context:component-scan>

当然,如果前缀的简称使用了其它方式的命名,那么在文档中要注意呼应。

<ctx:annotation-config/><ctx:component-scan>......</ctx:component-scan>

默认Namespance

注意文档第一行中

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

这里的语法有所不同,并没有设置自定义前缀。这说明在当前文档中,默认的Namespace是:http://www.springframework.org/schema/beans。

对于默认的Namespace中的元素,可以不使用前缀。

<bean id="viewResolver" class="......">

如果默认的Namespace有不同,要注意对XML做相应的调整。如将默认的Namespace设置为:http://www.springframework.org/schema/mvc

<bb:beans xmlns="http://www.springframework.org/schema/mvc"xmlns:bb="http://www.springframework.org/schema/beans"....>    .....    <annotation-driven>        <message-converters register-defaults="true">              <bb:bean class="org.springframework.http.converter.StringHttpMessageConverter">                   ......            </bb:bean>          </message-converters>    </annotation-driven>    <bb:bean id="viewResolver" class="......"></bb:beans>

xsi:schemaLocation的作用

元素中”xsi:schemaLocation”属性其实是Namespace为”http://www.w3.org/2001/XMLSchema-instance“里的schemaLocation属性,而XML文档已经声明了

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

所以这里可以使用xsi:schemaLocation(xsi=XmlSchemaInstance)。

schemaLocation提供了一个XML Namespace到对应的XSD文件的映射。

在xsi:schemaLocation后面配置的字符串都是成对的,前面的是Namespace的URI,后面是xsd文件的URI。两个URI之间以空白符分隔(空格和换行均可)。

Schema处理器将从指定的位置读取Schema文档,该schema文档的targetNamespace必须与第一个URI相匹配。例如:

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

这里表示Namespace为:http://www.springframework.org/schema/context的Schema的位置为:http://www.springframework.org/schema/context/spring-context.xsd。

通过浏览器打开这个xsd文档。下面是这个文档的开始部分:

<xsd:schema xmlns="http://www.springframework.org/schema/beans" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.springframework.org/schema/beans">

注意,这里的targetNamespace,是与我们写的Spring XML定义的Namespace相匹配的。

原创粉丝点击