最开始使用spring框架的时候,对于其配置文件xml,只是网上得知其使用方法,而不明其意。最近想着寻根问底的探究一下。以下是本文主要内容:

1、配置文件示例。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context.xsd
  13. http://www.springframework.org/schema/mvc
  14. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  15. http://www.springframework.org/schema/tx
  16. http://www.springframework.org/schema/tx/spring-tx.xsd
  17. http://www.springframework.org/schema/aop
  18. http://www.springframework.org/schema/aop/spring-aop.xsd "
  19. default-autowire="byName">
  20. </beans>

上 面是我们最常用的applicationContext.xml的配置文件的最开始部分,很多时候我们只是知道这些必须添加,但为什么,添加哪些都不甚清 楚。我们知道spring在启动的时候会验证 xml文档,这些引入的schema即用来验证配置文件的xml文档语法的正确性。下面先来看看如何验证xml文档验证的相关知识。

2、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、对于上述配置文件的详细解读

1)、 声明默认的名称空间(xmlns="http://www.springframework.org/schema/beans")

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

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:mvc 、xmlns:tx 、xmlns:context等等。

4、使用带版本号的xsd文件有何弊端?

首先来看看Spring是如何验证xml文档的。Spring默认在启动时是要加载XSD文件来验证xml文件的,所以如果有的时候断网了,或 者一些开源软件切换域名,那么就很容易碰到应用启动不了,为了防止这种情况,Spring提供了一种机制,默认从本地加载XSD文件。打开spring- context-4.1.2.RELEASE.jar,可以看到里面有两个特别的文件:
spring.handlers如下:
  1. http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
  2. http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler
  3. http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler
  4. http\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler
  5. http\://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler

spring.schemas如下:

  1. http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd
  2. http\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd
  3. http\://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd
  4. http\://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd
  5. http\://www.springframework.org/schema/context/spring-context-4.0.xsd=org/springframework/context/config/spring-context-4.0.xsd
  6. http\://www.springframework.org/schema/context/spring-context-4.1.xsd=org/springframework/context/config/spring-context-4.1.xsd
  7. http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.1.xsd
  8. http\://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd
  9. http\://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd
  10. http\://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd
  11. http\://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd
  12. http\://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd
  13. http\://www.springframework.org/schema/jee/spring-jee-4.0.xsd=org/springframework/ejb/config/spring-jee-4.0.xsd
  14. http\://www.springframework.org/schema/jee/spring-jee-4.1.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd
  15. http\://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd
  16. http\://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd
  17. http\://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd
  18. http\://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd
  19. http\://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd
  20. http\://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd
  21. http\://www.springframework.org/schema/lang/spring-lang-4.0.xsd=org/springframework/scripting/config/spring-lang-4.0.xsd
  22. http\://www.springframework.org/schema/lang/spring-lang-4.1.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd
  23. http\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd
  24. http\://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd
  25. http\://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd
  26. http\://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd
  27. http\://www.springframework.org/schema/task/spring-task-4.0.xsd=org/springframework/scheduling/config/spring-task-4.0.xsd
  28. http\://www.springframework.org/schema/task/spring-task-4.1.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd
  29. http\://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd
  30. http\://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd
  31. http\://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd
  32. http\://www.springframework.org/schema/cache/spring-cache-4.0.xsd=org/springframework/cache/config/spring-cache-4.0.xsd
  33. http\://www.springframework.org/schema/cache/spring-cache-4.1.xsd=org/springframework/cache/config/spring-cache-4.1.xsd
  34. http\://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-4.1.xsd

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

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

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

  1. http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.1.xsd
 

上述即为对spring配置文档中的xml 文档验证的一部分说明,也是我在学习过程中所忽略的部分

关于Spring配置文件xml文档的schema约束的更多相关文章

  1. Spring学习----- Spring配置文件xml文档的schema约束

    1.配置文件示例. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  2. Spring中xml文档的schema约束

    最开始使用Spring框架的时候,对于其配置文件xml,只是网上得知其使用方法,而不明其意.最近想着寻根问底的探究一下.以下是本文主要内容: 1.配置文件示例.   <?xml version= ...

  3. 关于Spring的xml文档的简单实用配置

    Spring的spring.xml文档的配置 最近在写Spring的配置文件时,发现Spring文档的配置其实没必要那么繁琐记忆,网上的很多文章都写得很繁琐,如果所有的东西按照路径去查找,可以很快的帮 ...

  4. 【HTML/XML 10】XML文档中的Schema文件

    导读:DTD是对XML文档进行有效性验证的方法之一,事实上,继DTD之后,出现了用来规范和描述XML文档的第二代标准:Schema.Schema是DTD的继承,但是也有其不同的地方,它是真正的以独立的 ...

  5. 使用JDom解析XML文档模拟Spring的配置文件解析

    在J2EE项目中可能会涉及到一些框架的使用,最近接触到了SSH,拿Spring来说配置文件的使用是相当重要的,Spring的配置文件是一个xml文件,Spring是如何读取到配置文件并进行依赖注入的呢 ...

  6. Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...

  7. 根据Schema写出XML文档四部曲

    Schema约束文档本身就是一个XML文档,扩展名为xsd 难点:XML文档的根元素怎么写? 如下4步曲: a.首先看Schema文档,找到根元素 <?xml version="1.0 ...

  8. 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    [转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...

  9. DTD与XML Schema都是XML文档。(选择1项)

    DTD与XML Schema都是XML文档.(选择1项) A.正确 B.不正确 解答:DTD不是XML文件, schema是XML文档

随机推荐

  1. 在node.js中建立你的第一个HTTp服务器

    这一章节我们将从初学者的角度介绍如何建立一个简单的node.js HTTP 服务器 创建myFirstHTTPServer.js //Lets require/import the HTTP modu ...

  2. (转)C/C++——auto,static,register,extern用法

    转自:https://blog.csdn.net/u010757264/article/details/49932829 C++中变量.函数的属性包括数据类型和存储类别.存储类别分为静态存储和动态存储 ...

  3. PHP读取xml方法介绍

    一,什么是xml,xml有什么用途 XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Marku ...

  4. 机器学习 F1-Score, recall, precision

    在机器学习,模式识别中,我们做分类的时候,会用到一些指标来评判算法的优劣,最常用的就是识别率,简单来说,就是 Acc=Npre/Ntotal 这里的 Npre表示预测对的样本数,Ntotal表示测试集 ...

  5. MySQL当月汇总 及负毛利汇总_20161027

    #当月汇总 及负毛利汇总 SELECT e.ID,e.city AS 城市 ,f.当月销售总额,f.当月成本总额,f.当月毛利总额,f.当月优惠券总额,f.当月赠品总额,f.当月毛利总额-f.当月优惠 ...

  6. ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)

    Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...

  7. mysql建表练习

    create table class( cid int primary key auto_increment, caption ) not null )engine=innodb; create ta ...

  8. kindle 3 webbrowser破解,

    首先下载破解: http://183.60.157.10/down_group83/M00/06/31/tzydCk3MyHYAAAAAAUTLHFYI-wk8562757/webpatch.7z?k ...

  9. Socket 阻塞与非阻塞模式

    http://blog.sina.com.cn/s/blog_5d0990c7010115ib.html

  10. 数据库路由中间件MyCat - 使用篇(4)

    此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 配置MyCat 3. 配置conf/rule.xml 1.5GA版本中的规则配置比较笨,2.0中优化了一些, ...