Spring中xml文档的schema约束
最开始使用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 "
- default-autowire="byName">
- </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文件有何弊端?
- 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.xsd=org/springframework/cache/config/spring-cache-4.1.xsd
再打开jar包里的org/springframework/context/config/ 目录,可以看到下面有
- http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.1.xsd
上述即为对spring配置文档中的xml 文档验证的一部分说明,也是我在学习过程中所忽略的部分。文中有参考一些视频教程还有一篇博文 为什么在Spring的配置里,最好不要配置xsd文件的版本号。
Quartzhao的专栏
Spring中xml文档的schema约束的更多相关文章
- Spring学习----- Spring配置文件xml文档的schema约束
1.配置文件示例. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- 关于Spring配置文件xml文档的schema约束
最开始使用spring框架的时候,对于其配置文件xml,只是网上得知其使用方法,而不明其意.最近想着寻根问底的探究一下.以下是本文主要内容: 1.配置文件示例. <?xml version=&q ...
- 关于Spring的xml文档的简单实用配置
Spring的spring.xml文档的配置 最近在写Spring的配置文件时,发现Spring文档的配置其实没必要那么繁琐记忆,网上的很多文章都写得很繁琐,如果所有的东西按照路径去查找,可以很快的帮 ...
- Delphi6/7 中XML 文档的应用
XML文档是新一代的Web数据格式.它可以用树的形式储存一切数据.下面介绍一下TXMLDocument控件的一些用法:已定义:XMLDoc: TXMLDocument;EncoderMIME: TId ...
- C#中XML文档注释编译DLL引用到其它项目
引用地址:http://zhidao.baidu.com/link?url=jSGYEBysE4gBExtNsHCVk3vd2OK2cMlaf02cS79GdRuGueTBdFJB0btOdBYkg_ ...
- 【HTML/XML 10】XML文档中的Schema文件
导读:DTD是对XML文档进行有效性验证的方法之一,事实上,继DTD之后,出现了用来规范和描述XML文档的第二代标准:Schema.Schema是DTD的继承,但是也有其不同的地方,它是真正的以独立的 ...
- XML文档中的xmlns、xmlns:xsi和xsi:schemaLocation
文章转载自:https://yq.aliyun.com/articles/40353 相信很多人和我一样,在编写Spring或者Maven或者其他需要用到XML文档的程序时,通常都是将这些XML文档头 ...
- 使用JDom解析XML文档模拟Spring的配置文件解析
在J2EE项目中可能会涉及到一些框架的使用,最近接触到了SSH,拿Spring来说配置文件的使用是相当重要的,Spring的配置文件是一个xml文件,Spring是如何读取到配置文件并进行依赖注入的呢 ...
- 根据Schema写出XML文档四部曲
Schema约束文档本身就是一个XML文档,扩展名为xsd 难点:XML文档的根元素怎么写? 如下4步曲: a.首先看Schema文档,找到根元素 <?xml version="1.0 ...
随机推荐
- spi简介(自我理解)
因为项目中用到spi总线,网上看了下资料,总感觉云山雾罩的,就向身边的同事问了下,他给我解释了下, 我现在把自己理解的写下来 spi一共四条线,一条选择线,一条数据线,2条数据线.spi是一对多设备, ...
- Django-xadmin+django-import-export导入导出的实现
转载自:https://blog.csdn.net/zcyuefan/article/details/77748850 1. 需求vs现状1.1 需求要求做一个ERP后台辅助管理的程序,有以下几项基本 ...
- Ribbon 负载均衡
一.是什么 Spring Cloud Ribbon 是基于 Netfix Ribbon 实现的一套客户端(服务的消费者)负载均衡的工具 二.用法 服务消费者工程的 pom.xml 添加如下内容 < ...
- xtrabackup增量备份mysql +MHA
http://blog.csdn.net/yanggd1987/article/category/2214421 https://www.centos.bz/2013/09/innobackupex- ...
- Android之怎样更改获取焦点的先后顺序
在组件中增加<requestFocus />能够首先获得焦点 以TextView为例: 例如以下: <TextView android:layout_width=&q ...
- 热修复JSPatch之实战教程
接上篇<热修复JSPatch之接口设计>,在这篇文章主要给大家讲述一下怎样高速具备热修复能力,当然了假设有人有志于把JSPatch系统的学习,甚至用JSPatch进行开发的.就没有必要 ...
- 基于ArcGIS Flex API实现动态标绘(1.0)
标绘作为一种数据展示形式,在多个行业都有需求. 基于ArcGIS Flex API(3.6)实现标绘API,当前版本号1.0 alpha,支持经常使用几种标绘符号,包含: 圆弧.曲线.圆形.椭圆.弓形 ...
- Swagger框架学习分享
Swagger框架学习分享 转至元数据结尾 Created and last modified by 刘新宇 大约1分钟曾经 pageId=162045803#page-metadata-start& ...
- luogu2429 制杖题
题目大意 求不大于 m 的. 质因数集与给定有n个元素的质数集有交集的自然数之和. 数据范围 1 2 3 n*m<=10^7 4 5 n<=2,m<=10^9 6 7 n<=2 ...
- CodeForces 131C C (组合)
There are n boys and m girls attending a theatre club. To set a play "The Big Bang Theory" ...