Spring配置文件头及xsd文件版本

最初Spring配置文件的头部声明如下:

 Xml代码  收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"  
  3. "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  4. <beans>  
  5.   
  6. </beans>  
  7.   
  8.   
  9. 说明:  
  10. 1、第一行表示xml声明,任何格式良好的xml文档都必须第一行是声明。相当于告诉解析器这个是xml文档,你给我用xml解析器解析。  
  11.   
  12. 2、dtd声明,表示该xml里的元素和属性等需符合spring-beans-2.0.dtd这个文档类型定义标准。  
  13.   
  14.   
  15.   
  16. DTD:文件的文件型别定义(Document Type Definition)可以看成一个或者多个 XML 文件的模板,在这里可以定义 XML 文件中的元素、元素的属性、元素的排列方式、元素包含的内容等等。  

 

 因为DTD的一些局限性,以及XML Schema对数据类型和命名空间的支持。XML Schema很快会将 DTD 取而代之

 被XML Schema 取代后的Spring 配置:

 Xml代码  收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="  
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  10.   
  11. </beans>  
  12.   
  13.   
  14. XML Schema命名空间作用:  
  15. 1、避免命名冲突,像Java中的package一样  
  16.   
  17. 2、将不同作用的标签分门别类(像Spring中的tx命名空间针对事务类的标签,context命名空间针对组件的标签)  
  18.   
  19.   
  20. 代码解释:  
  21. 1、xmlns="http://www.springframework.org/schema/beans"  
  22. 声明xml文件默认的命名空间,表示未使用其他命名空间的所有标签的默认命名空间。  
  23.   
  24. 2、xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  25. 声明XML Schema 实例,声明后就可以使用 schemaLocation 属性了  
  26.   
  27. 3、xmlns:aop="http://www.springframework.org/schema/aop"  
  28. 声明前缀为aop的命名空间,后面的URL用于标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。  
  29.   
  30. 4、xsi:schemaLocation="  
  31. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  32. 这个从命名可以看出个大概,指定Schema的位置这个属性必须结合命名空间使用。这个属性有两个值,第一个值表示需要使用的命名空间。第二个值表示供命名空间使用的 XML schema 的位置  
  33.   
  34.   
  35. 所以我们需要什么样的标签的时候,就引入什么样的命名空间和Schema 定义就可以了。  

XSD有没有版本号的区别

通常情况下,namespace对应的URI是一个存放XSD的地址,尽管规范没有这么要求。如果没有提供schemaLocation,那么Spring的XML解析器会从namespace的URI里加载XSD文件。我们可以把配置文件改成这个样子,也是可以正常工作的:

 

 

[html] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans/spring-beans.xsd"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  

schemaLocation提供了一个xml namespace到对应的XSD文件的一个映射,所以我们可以看到,在xsi:schemaLocation后面配置的字符串都是成对的,前面的是namespace的URI,后面是xsd文件的URI。比如:

 

 

[html] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  2. http://www.springframework.org/schema/beans/spring-beans.xsd  
  3. http://www.springframework.org/schema/security  
  4. http://www.springframework.org/schema/security/spring-security.xsd"  

 

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

spring.handlers

 

[plain] view plain copy

 在CODE上查看代码片派生到我的代码片

  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

 

[plain] view plain copy

 在CODE上查看代码片派生到我的代码片

  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.xsd=org/springframework/context/config/spring-context-3.2.xsd  
  6. ...  

 

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

spring-context-2.5.xsd
spring-context-3.0.xsd
spring-context-3.1.xsd
spring-context-3.2.xsd

很明显,可以想到Spring是把XSD文件放到本地了,再在spring.schemas里做了一个映射,优先从本地里加载XSD文件。

并且Spring很贴心,把旧版本的XSD文件也全放了。这样可以防止升级了Spring版本,而配置文件里用的还是旧版本的XSD文件,然后断网了,应用启动不了。

我们还可以看到,在没有配置版本号时,用的就是当前版本的XSD文件:

 

[html] view plain copy

 在CODE上查看代码片派生到我的代码片

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

结论:不要在Spring的配置里,配置上XSD的版本号,因为如果没有配置版本号,取的就是当前jar里的XSD文件,减少了各种风险。而且这样约定大于配置的方式很优雅。

同样,我们打开dubbo的jar包,可以在它的spring.schemas文件里看到有这样的配置:

 

 

[html] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd  

所以,Spring在加载dubbo时,会从dubbo的jar里加载dubbo.xsd。

 

如何跳过Spring的XML校验?

 

可以用这样的方式来跳过校验:

 

[html] view plain copy

 在CODE上查看代码片派生到我的代码片

  1. GenericXmlApplicationContext context = new GenericXmlApplicationContext();  
  2. context.setValidating(false);  

如何写一个自己的spring xml namespace扩展

可以参考Spring的文档,实际上是相当简单的。只要实现自己的NamespaceHandler,再配置一下spring.handlers和spring.schemas就可以了。

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/extensible-xml.html

猜你喜欢

转载自blog.csdn.net/qq_37634294/article/details/82222427
今日推荐