查看原文
其他

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

点击关注 👉 Java面试那些事儿 2022-04-29

大家好,我是D哥

点击关注下方公众号,Java面试资料 都在这里

作者:南宫酥卿
来源: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.ContextNamespaceHandlerhttp\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandlerhttp\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandlerhttp\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandlerhttp\://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.xsdhttp\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsdhttp\://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsdhttp\://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsdhttp\://www.springframework.org/schema/context/spring-context-4.0.xsd=org/springframework/context/config/spring-context-4.0.xsdhttp\://www.springframework.org/schema/context/spring-context-4.1.xsd=org/springframework/context/config/spring-context-4.1.xsdhttp\://www.springframework.org/schema/context/spring-context-4.2.xsd=org/springframework/context/config/spring-context-4.2.xsdhttp\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.2.xsd

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


技术交流群


最后,D哥也建了一个技术群,主要探讨一些新的技术和开源项目值不值得去研究及IDEA使用的“骚操作”,有兴趣入群的同学,可长按扫描下方二维码,一定要备注:城市+昵称+技术方向,根据格式备注,可更快被通过且邀请进群。


▲长按扫描


热门推荐:

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存