Spring 配置文件xml文档的schema约束

来源:互联网 发布:召唤师捏脸数据 编辑:程序博客网 时间:2024/05/21 05:44

在使用spring框架的时候,对于其配置文件xml,只是知道其使用方法,而不知道为什么。

这里来研究一下下。

1.配置文件示例

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"            xmlns:mvc="http://www.springframework.org/schema/mvc"         xmlns:tx="http://www.springframework.org/schema/tx"      xmlns:aop="http://www.springframework.org/schema/aop"      xmlns:context="http://www.springframework.org/schema/context"      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.xsd                http://www.springframework.org/schema/context                 http://www.springframework.org/schema/context/spring-context.xsd                http://www.springframework.org/schema/mvc                http://www.springframework.org/schema/mvc/spring-mvc.xsd              http://www.springframework.org/schema/tx               http://www.springframework.org/schema/tx/spring-tx.xsd              http://www.springframework.org/schema/aop              http://www.springframework.org/schema/aop/spring-aop.xsd ">  </beans>  

上面是我们最常用的 applicationContext.xml 的配置文件的头部,很多时候我们只是知道这些必须添加,但为什么,添加哪些都不知道。

我们知道spring在启动的时候会验证 xml文档,这些引入的schema即用来验证配置文件的xml文档语法的正确性。

下面先来看看如何验证xml文档验证的相关知识。

2.xml的schema约束

schema定义

Schema的作用是定义一份XML文档中的可以正确使用的标签组。

就像文档类型定义(document type declaration 外语缩写:DTD)的作用一样,一份XML Schema定义了:

  1. 可以出现在文档里的元素;
  2. 可以出现在文档里的属性;
  3. 哪些元素是子元素;
  4. 子元素的顺序;
  5. 子元素的数量;
  6. 一个元素应是否能包含文本,或应该是空的;
  7. 元素和属性的数据类型;
  8. 元素和属性的默认值和固定值。

XML Schema 是 DTD 的升级版

XML Schema 是用来取代 DTD 的 。

XML Schema 优于DTD的地方:

  1. XML Schema 可针对未来的需求进行扩展
  2. XML Schema 更完善,功能更强大
  3. XML Schema 基于 XML 编写
  4. XML Schema 支持数据类型
  5. XML Schema 支持命名空间

先来说说xml文档的schema约束,它定义了xml文档的结构,内容和语法,包括元素和属性、关系的结构以及数据类型等。

有以下几点需要遵循:

  1. 所有的标签和属性都需要Schema来定义。(schema本身由w3c来定义)。

  2. 所有的schema文件都需要以个ID,这里我们称之为 namespace,其值时一个url,通常是这个xml的xsd文件的地址。

  3. namespace值由 targetNamespace 属性来指定

  4. 引入一个schema约束,使用属性xmlns,属性值即为对应schema文件的命名空间 nameSpace。

  5. 如果引入的schema非w3c组织定义的,必须指定schema文件的位置,schema文件的位置由 schemaLocation来指定。

  6. 引入多个schema文件需要使用别名。别名形式如下: xmlns:alias。

3.配置文件的详细解读

标签 说明 xmlns xml NameSpace : xml文件命名空间 xsi xml schema instance : xml Schema 实例 xsi:schemaLocation 指定命名空间 + Schema文件的位置 xmlns:alias xmlns:alias : 命名空间的别名
  1. 声明默认的名称空间(xmlns="http://www.springframework.org/schema/beans"

  2. 声明XML Schema实例名称空间(http://www.w3.org/2001/XMLSchema-instance),并将xsi前缀与该名称空间绑定, 这样模式处理器就可以识别xsi:schemaLocation属性。XML Schema实例名称空间的前缀通常使用xsi(xml schema instance)

  3. 使用xsi:schemaLocation属性指定名称空间(http://www.springframework.org/schema/beans) 和模式位置(http://www.springframework.org/schema/beans/spring-beans.xsd)相关。 XML Schema推荐标准中指出,xsi:schemaLocation属性可以在实例中的任何元素上使用,而不一定是根元素,不过,xsi:schemaLocation 属性必须出现在它要验证的任何元素和属性之前。

  4. 使用别名引入多个schema文件。如上述 xmlns:mvcxmlns:txxmlns:context等等。例如:xmlns:mvc="http://www.springframework.org/schema/mvc" 代表之后使用mvc代表"http://www.springframework.org/schema/mvc"这个命名空间。

<mvc:annotation-driven />

代表着:

<http://www.springframework.org/schema/mvc:annotation-driven />

4.Spring的xsd文件不要带版本号

首先来看看Spring是如何验证xml文档的。Spring默认在启动时是要加载XSD文件来验证xml文件的,所以如果有的时候断网了,或者一些开源软件切换域名,那么就很容易碰到应用启动不了,为了防止这种情况,Spring 提供了一种机制,默认从本地加载XSD文件。打开spring- context-4.2.2.RELEASE.jar,可以看到里面有两个特别的文件:

spring.handlers如下:

http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler  http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler  http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler  http\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler  http\://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler 

spring.schemas 如下:

http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd  http\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd  http\://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd  http\://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd  http\://www.springframework.org/schema/context/spring-context-4.0.xsd=org/springframework/context/config/spring-context-4.0.xsd  http\://www.springframework.org/schema/context/spring-context-4.1.xsd=org/springframework/context/config/spring-context-4.1.xsd  http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.1.xsd  http\://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd  http\://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd  http\://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd  http\://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd  http\://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd  http\://www.springframework.org/schema/jee/spring-jee-4.0.xsd=org/springframework/ejb/config/spring-jee-4.0.xsd  http\://www.springframework.org/schema/jee/spring-jee-4.1.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd  http\://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd  http\://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd  http\://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd  http\://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd  http\://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd  http\://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd  http\://www.springframework.org/schema/lang/spring-lang-4.0.xsd=org/springframework/scripting/config/spring-lang-4.0.xsd  http\://www.springframework.org/schema/lang/spring-lang-4.1.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd  http\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd  http\://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd  http\://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd  http\://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd  http\://www.springframework.org/schema/task/spring-task-4.0.xsd=org/springframework/scheduling/config/spring-task-4.0.xsd  http\://www.springframework.org/schema/task/spring-task-4.1.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd  http\://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd  http\://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd  http\://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd  http\://www.springframework.org/schema/cache/spring-cache-4.0.xsd=org/springframework/cache/config/spring-cache-4.0.xsd  http\://www.springframework.org/schema/cache/spring-cache-4.1.xsd=org/springframework/cache/config/spring-cache-4.1.xsd  http\://www.springframework.org/schema/cache/spring-cache-4.2.xsd=org/springframework/cache/config/spring-cache-4.2.xsd  http\://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-4.2.xsd  

再打开jar包里的 org/springframework/context/config/ 目录,可以看到下面有:

这里写图片描述

很明显,可以想到 Spring 是把XSD文件放到本地了,再在spring.schemas里做了一个映射,优先从本地里加载XSD文件。并且 Spring 很贴心,把旧版本的XSD文件也全缓存了。这样可以防止升级了Spring版本,而配置文件里用的还是旧版本的XSD文件,然后断网了,应用启动不了。

http\://www.springframework.org/schema/cache/spring-cache-4.2.xsd=org/springframework/cache/config/spring-cache-4.2.xsd  http\://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-4.2.xsd  

从上述xml文档中可以看出,当没有配置版本号时,使用的即当前版本的xml schema文档。

http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.2.xsd
0 0
原创粉丝点击