为编程爱好者分享易语言教程源码的资源网
好用的代理IP,游戏必备 ____广告位招租____ 服务器99/年 ____广告位招租____ ____广告位招租____ 挂机,建站服务器
好用的代理IP,游戏必备 ____广告位招租____ 服务器低至38/年 ____广告位招租____ ____广告位招租____ 挂机,建站服务器

网站首页 > 网络编程 正文

开玩笑吧!写这么久的Spring Xml配置,竟然连xsd和dtd都不知道?

三叶资源网 2022-09-03 20:18:50 网络编程 230 ℃ 0 评论

作者:南宫素卿

来源:https://blog.csdn.net/win7system/article/details/91553401


最近,在品读mybatis源码的时候,发现以前做项目的时候,所有xml配置文件一直都是直接从一个项目中拷贝到另一个项目中,配置文件的头部也不知道干嘛用的,感觉可有可无,当debug追踪Spring IOC加载sqlsessionFactory的时候,发现这个文件头部是用来检查xml的,随着Spring等相关版本升高,原先的功能有遗弃的,有新增的,在按照往常的拷贝,势必会留下隐患。


本文将结合网上已有知识点和自己的理解来解释下他们的来龙去脉。


1、DTD 现在基本已被XSD文档取代,但是,仍有个别在使用 比如 Mybatis mapper xml文件

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


上面用的是公共DTD,DTD名称格式为"注册//组织//类型 标签//语言"


"注册" 指示组织是否由国际标准化组织(ISO)注册,+表示是,-表示不是;


"组织" 即组织名称,如:mybatis.org;


"类型" 一般是DTD;


"标签" 是指定公开文本描述,即对所引用的公开文本的唯一描述性名称,后面可附带版本号,如Mapper 3.0。


"语言" EN指英语;


http://mybatis.org/dtd/mybatis-3-mapper.dtd 表示外部DTD文件URI,可以在此处下载,不过,Mybatis加载xml文件之前会在本地加载DTD代替从线上获取。


内部DTD 是使用<!DOCTYPE rootElement [xxx]> 来定义的,当然内部DTD也可以使用DTD文件,如<!DOCTYPE rootElement SYSTEM "URI">

<?xml version="1.0"?>
<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body)>
  <!ELEMENT to      (#PCDATA)>
  <!ELEMENT from    (#PCDATA)>
  <!ELEMENT heading (#PCDATA)>
  <!ELEMENT body    (#PCDATA)>
]>
<note>
  <to>George</to>
  <from>John</from>
  <heading>Reminder</heading>
  <body>Don't forget the meeting!</body>
</note>


2、XSD 现在基本是主流,功能自然比DTD全面且优秀,以Spring为例

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:tx="http://www.springframework.org/schema/tx"
   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/tx   
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.0.xsd">


首先说明最基本的头部命名空间信息,配置文件必须的部分,固定部分

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


声明xml文件默认的命名空间,表示未使用其他命名空间的所有标签的默认命名空间。


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


声明XML Schema 实例,声明后就可以使用 schemaLocation属性了。


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


这是spring配置文件里面需要使用到context的标签,声明前缀为context的命名空间,后面的URL用于标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称,在容器启动的时候找到对应的命名空间处理器处理。当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。然后其他比如context(针对组件标签)、MVC(针对mvc标签)、tx(针对事务标签)都一样的意思。


xsi:schemaLaction部分:


http://www.springframework.org/schema/beans --- 表示区别命名空间url


http://www.springframework.org/schema/beans/spring-beans-4.0.xsd --- 表示命名空间对应xsd文件获取地址(也只是一个映射地址)


是为上面配置的命名空间指定xsd规范文件,访问这个地址你会发现,仍是在本地获取对应xsd文件。这样你在进行下面具体配置的时候就会根据这些xsd规范文件给出相应的提示,比如说每个标签是怎么写的,都有些什么属性是都可以智能提示的,以防配置中出错而不太容易排查,在启动服务的时候也会根据xsd规范对配置进行校验。


容器启动时查找:


1、命名空间url和命名空间处理映射(以content为例)

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


2、xsd文件地址映射Spring框架本地(以content为例)

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-4.2.xsd=org/springframework/context/config/spring-context-4.2.xsd
http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.2.xsd

希望本文能帮初学者扫盲。

Tags:

来源:三叶资源网,欢迎分享,公众号:iisanye,(三叶资源网⑤群:21414575

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

百度站内搜索
关注微信公众号
三叶资源网⑤群:三叶资源网⑤群

网站分类
随机tag
自定义数据类型剖析卷帘式菜单LOL无限视距动态加密YY自定义颜色EXUIBCC校验sqlcipher数据库控制托盘图标POST上传adb模块源码OPENSSL加密解密集福卡普通人学python有意义吗ACCESS操作python list改名隐藏外部程序纯组件实现圆形头像Web浏览器11111
最新评论