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


 http://iswift.iteye.com/blog/1657537

 





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM