Spring配置文件頭及xsd文件版本淺析


一、Spring配置文件頭

最初Spring配置文件的頭部聲明如下:

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>

說明:  

1、第一行表示xml聲明,任何格式良好的xml文檔都必須第一行是聲明。相當於告訴解析器這個是xml文檔,你給我用xml解析器解析。

2、dtd聲明,表示該xml里的元素和屬性等需符合spring-beans-2.0.xsd這個文檔類型定義標准。 

3、DTD:文件的文件類型定義(Document Type Definition)可以看成一個或者多個 XML 文件的模板,在這里可以定義 XML 文件中的元素、元素的屬性、元素的排列方式、元素包含的內容等等。

因為DTD的一些局限性,以及XML Schema對數據類型和命名空間的支持。XML Schema很快將 DTD 取而代之

被XML Schema 取代后的Spring 配置:

 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"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 8        http://www.springframework.org/schema/context
 9        http://www.springframework.org/schema/context/spring-context-4.2.xsd
10        http://www.springframework.org/schema/aop
11        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
12 
13 </beans>

 

XML Schema命名空間作用:  

1、避免命名沖突,與Java中的package類似

2、將不同作用的標簽分門別類(像Spring中的tx命名空間針對事務類的標簽,context命名空間針對組件的標簽)  

 

代碼解釋:
1、xmlns="http://www.springframework.org/schema/beans"
聲明xml文件默認的命名空間,表示未使用其他命名空間的所有標簽的默認命名空間。

2、xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
聲明XML Schema 實例,聲明后就可以使用 schemaLocation 屬性了

3、xmlns:aop="http://www.springframework.org/schema/aop"
聲明前綴為aop的命名空間,后面的URL用於標示命名空間的地址不會被解析器用於查找信息。其惟一的作用是賦予命名空間一個惟一的名稱。當命名空間被定義在元素的開始標簽中時,所有帶有相同前綴的子元素都會與同一個命名空間相關聯。

4、xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
這個從命名可以看出個大概,指定Schema的位置這個屬性必須結合命名空間使用。這個屬性有兩個值,第一個值表示需要使用的命名空間。第二個值表示供命名空間使用的 XML schema 的位置

所以我們需要什么樣的標簽的時候,就引入什么樣的命名空間和Schema 定義就可以。

二、XSD有沒有版本號的區別

 通常情況下,namespace對應的URI是一個存放XSD的地址

 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"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7        http://www.springframework.org/schema/beans/spring-beans.xsd
 8        http://www.springframework.org/schema/context
 9        http://www.springframework.org/schema/context/spring-context.xsd
10        http://www.springframework.org/schema/aop
11        http://www.springframework.org/schema/aop/spring-aop.xsd">
12 
13 </beans>

schemaLocation提供了一個xml namespace到對應的XSD文件的一個映射,所以我們可以看到,在xsi:schemaLocation后面配置的字符串都是成對的,前面的是namespace的URI,后面是xsd文件的URI。

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

spring.handlers

 

spring.schemas

再打開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-context-4.0.xsd
spring-context-4.1.xsd
spring-context-4.2.xsd

 

很明顯,可以想到Spring是把XSD文件放到本地了,再在spring.schemas里做了一個映射,優先從本地里加載XSD文件。

並且Spring很貼心,把舊版本的XSD文件也全放了。這樣可以防止升級了Spring版本,而配置文件里用的還是舊版本的XSD文件,然后斷網了,應用啟動不了。

我們還可以看到,在沒有配置版本號時,用的就是當前版本的XSD文件:

 

結論:不要在Spring的配置文件中配置XSD的版本號,因為沒配置版本號時,默認取當前jar里的XSD文件,減少了各種風險。而且這樣約定大於配置的方式很優雅

 


免責聲明!

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



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