關於Spring配置文件xml文檔的schema約束


最開始使用spring框架的時候,對於其配置文件xml,只是網上得知其使用方法,而不明其意。最近想着尋根問底的探究一下。以下是本文主要內容:

1、配置文件示例。

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"        
  3.     xmlns:mvc="http://www.springframework.org/schema/mvc"     
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"              
  8.     xsi:schemaLocation="                                               
  9.             http://www.springframework.org/schema/beans    
  10.             http://www.springframework.org/schema/beans/spring-beans.xsd    
  11.             http://www.springframework.org/schema/context     
  12.             http://www.springframework.org/schema/context/spring-context.xsd    
  13.             http://www.springframework.org/schema/mvc    
  14.             http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  15.             http://www.springframework.org/schema/tx   
  16.             http://www.springframework.org/schema/tx/spring-tx.xsd  
  17.             http://www.springframework.org/schema/aop  
  18.             http://www.springframework.org/schema/aop/spring-aop.xsd "  
  19.     default-autowire="byName">  
  20. </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文件有何弊端?

首先來看看Spring是如何驗證xml文檔的。Spring默認在啟動時是要加載XSD文件來驗證xml文件的,所以如果有的時候斷網了,或 者一些開源軟件切換域名,那么就很容易碰到應用啟動不了,為了防止這種情況,Spring提供了一種機制,默認從本地加載XSD文件。打開spring- context-4.1.2.RELEASE.jar,可以看到里面有兩個特別的文件:

spring.handlers如下:
  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如下:
  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-4.0.xsd=org/springframework/context/config/spring-context-4.0.xsd  
  6. http\://www.springframework.org/schema/context/spring-context-4.1.xsd=org/springframework/context/config/spring-context-4.1.xsd  
  7. http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.1.xsd  
  8. http\://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd  
  9. http\://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd  
  10. http\://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd  
  11. http\://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd  
  12. http\://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd  
  13. http\://www.springframework.org/schema/jee/spring-jee-4.0.xsd=org/springframework/ejb/config/spring-jee-4.0.xsd  
  14. http\://www.springframework.org/schema/jee/spring-jee-4.1.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd  
  15. http\://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd  
  16. http\://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd  
  17. http\://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd  
  18. http\://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd  
  19. http\://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd  
  20. http\://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd  
  21. http\://www.springframework.org/schema/lang/spring-lang-4.0.xsd=org/springframework/scripting/config/spring-lang-4.0.xsd  
  22. http\://www.springframework.org/schema/lang/spring-lang-4.1.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd  
  23. http\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd  
  24. http\://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd  
  25. http\://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd  
  26. http\://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd  
  27. http\://www.springframework.org/schema/task/spring-task-4.0.xsd=org/springframework/scheduling/config/spring-task-4.0.xsd  
  28. http\://www.springframework.org/schema/task/spring-task-4.1.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd  
  29. http\://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd  
  30. http\://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd  
  31. http\://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd  
  32. http\://www.springframework.org/schema/cache/spring-cache-4.0.xsd=org/springframework/cache/config/spring-cache-4.0.xsd  
  33. http\://www.springframework.org/schema/cache/spring-cache-4.1.xsd=org/springframework/cache/config/spring-cache-4.1.xsd  
  34. http\://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-4.1.xsd  
再打開jar包里的org/springframework/context/config/ 目錄,可以看到下面有
很明顯,可以想到Spring是把XSD文件放到本地了,再在spring.schemas里做了一個映射,優先從本地里加載XSD文件。並且 Spring很貼心,把舊版本的XSD文件也全放了。這樣可以防止升級了Spring版本,而配置文件里用的還是舊版本的XSD文件,然后斷網了,應用啟 動不了。

從上述xml文檔中可以看出,當沒有配置版本號時,使用的即當前版本的xml schema文檔。

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

上述即為對spring配置文檔中的xml 文檔驗證的一部分說明,也是我在學習過程中所忽略的部分。文中有參考一些視頻教程還有一篇博文 為什么在Spring的配置里,最好不要配置xsd文件的版本號


免責聲明!

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



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