spring16-----XML命名空間和Spring配置文件中的頭
一. 什麽是命名空間
在 XML 中,元素名稱是由開發者定義的,當兩個不同的文檔使用相同的元素名時,就會發生命名沖突。類似package的作用。
這個 XML 文檔攜帶著某個表格中的信息:
1 <table> 2 <tr> 3 <td>Apples</td> 4 <td>Bananas</td> 5 </tr> 6 </table>View Code
這個 XML 文檔攜帶有關桌子的信息(一件家具):
1 <table> 2 <name>African Coffee Table</View Codename> 3 <width>80</width> 4 <length>120</length> 5 </table>
假如這兩個 XML 文檔被一起使用,由於兩個文檔都包含帶有不同內容和定義的 <table> 元素,就會發生命名沖突。
XML 解析器無法確定如何處理這類沖突。
1. 使用前綴來避免命名沖突
此文檔帶有某個表格中的信息:
1 <h:table> 2 <h:tr> 3 <h:td>Apples</h:td> 4 <View Codeh:td>Bananas</h:td> 5 </h:tr> 6 </h:table>
此 XML 文檔攜帶著有關一件家具的信息:
1 <f:table> 2 <f:name>African Coffee Table</f:name> 3 <f:width>80</f:width> 4 <f:length>120</f:length> 5 </f:table>View Code
現在,命名沖突不存在了,這是由於兩個文檔都使用了不同的名稱來命名它們的 <table> 元素 (<h:table> 和 <f:table>)。
通過使用前綴,我們創建了兩種不同類型的 <table> 元素。
2. 使用命名空間(Namespaces)
這個 XML 文檔攜帶著某個表格中的信息:
1 <h:table xmlns:h="http://www.w3.org/TR/html4/"> 2 <h:tr> 3 <h:td>Apples</h:td> 4 <h:td>Bananas</h:td> 5 </h:tr> 6 </h:table>View Code
此 XML 文檔攜帶著有關一件家具的信息:
1 <f:table xmlns:f="http://www.w3school.com.cn/furniture"> 2 <f:name>African Coffee Table</f:name> 3 <f:width>80</f:width> 4 <f:length>120</f:length> 5 </f:table>View Code
與僅僅使用前綴不同,我們為 <table> 標簽添加了一個 xmlns 屬性,這樣就為前綴賦予了一個與某個命名空間相關聯的限定名稱。
3. XML Namespace(xmlns)屬性
XML 命名空間屬性被放置於元素的開始標簽之中,並使用以下的語法:
xmlns:namespace-prefix="namespaceURI" (note:有時候prefix可以省略,直接xmlns="",比如默認的命名空間)
當命名空間被定義在元素的開始標簽中時,所有帶有相同前綴的子元素都會與同一個命名空間相關聯。
註釋:用於標示命名空間的地址不會被解析器用於查找信息。其惟一的作用是賦予命名空間一個惟一的名稱。不過,很多公司常常會作為指針來使用命名空間指向實際存在的網頁,這個網頁包含關於命名空間的信息。
4. XML命名空間的聲明
xmlns:類似於一個保留字,它只用於聲明命名空間
eg:
xmlns:aop="http://www.springframework.org/schema/aop"
aop:這裏實際上是將前綴“aop”與命名空間"http://www.springframework.org/schema/aop"(這個URI包含關於命名空間的信息)綁定在一起。通常我們會用一個比較簡短或約定俗成的名字來作為命名空間的前綴(例如這裏的aop),但具體使用什麽前綴完全取決於個人.自定義命名空間的前綴是合法的。使用有意義的命名空間前綴增強了XML檔的清晰性。所以可以看到我們平時在配置Spring配置文件的時候,前綴名都是aop(切面)、tx(事務)等命名方式。
5. 重要的配置
比如spring中的:
首先xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance 是必須有的。
xsi:schemaLocation:為指定了用於解析和校驗xml的定義文件(xsd)的位置。
xmlns是xml命名空間,xmlns:xsi是指xml所遵守的標簽規範
6. 指明命名空間的Schema文件地址的用途
指明命名空間的Schema 文件地址有兩個用途:
1、XML解析器可以獲取Schema文件並對文檔進行格式化驗證;
2、在開發環境下,Ide(如eclipse)可以引用Schema文件對文檔編輯器提供誘導功能,如自動完成提示。
二. Spring的命名空間
Spring命名空間有13個,分別如下:
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:aop="http://www.springframework.org/schema/aop" 5 xmlns:c="http://www.springframework.org/schema/c" 6 xmlns:cache="http://www.springframework.org/schema/cache" 7 xmlns:context="http://www.springframework.org/schema/context" 8 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 9 xmlns:jee="http://www.springframework.org/schema/jee" 10 xmlns:lang="http://www.springframework.org/schema/lang" 11 xmlns:mvc="http://www.springframework.org/schema/mvc" 12 xmlns:p="http://www.springframework.org/schema/p" 13 xmlns:task="http://www.springframework.org/schema/task" 14 xmlns:tx="http://www.springframework.org/schema/tx" 15 xmlns:util="http://www.springframework.org/schema/util" 16 xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd 17 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 18 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 19 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 20 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd 21 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd 22 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd 23 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd 24 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 25 http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd 26 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "> 27 28 29 </beans>View Code
三. Spring找到校驗XML的xsd文件
Spring默認在啟動時是要從配置的命名空間的位置加載XSD文件來驗證xml文件的,所以如果有的時候斷網了,或者一些開源軟件切換域名,那麽就很容易碰到應用啟動不了。
為了防止這種情況,Spring提供了一種機制,即默認從本地加載XSD文件,當本地沒有時才根據實際的URI去聯網獲得。
我們打開Spring-aop-4.1.6RELEASE.jar (這是我本地的版本),這個包下有一個META_INF文件夾,其中有兩個文件:spring.handlers和spring.schemas。
spring.handlers
1 |
http\: //www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler
|
spring.schemas
http\://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd http\://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd http\://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd http\://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd http\://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd http\://www.springframework.org/schema/aop/spring-aop-4.0.xsd=org/springframework/aop/config/spring-aop-4.0.xsd http\://www.springframework.org/schema/aop/spring-aop-4.1.xsd=org/springframework/aop/config/spring-aop-4.1.xsd http\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-4.1.xsd
我們看到一個xsd文件對應本地的一個路徑,我們打開org/springframework/aop/config/可以看到:
這就很明顯,Spring是把XSD文件放到本地了,再在spring.schemas裏做了一個映射,優先從本地裏加載XSD文件。
並且把spring舊版本的XSD文件也全放了。這樣可以防止升級了Spring版本,而配置文件裏用的還是舊版本的XSD文件,然後斷網了,應用啟動不了。
註意我在spring.schemas中標紅的最後一行,說明我們在寫命名空間值對應的xsd文件位置時,可以不用寫版本號,它默認的是本地spring相關版本的對應xsd版本,我這裏是4.1。
在xsi:schemaLocation中這樣寫:http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
總結一句話就是:命名空間是為了防止相同標簽解析沖突(添加命名空間相當於添加了前綴來隔開),而shema對應的地址(.xsd文件)規定了文件應該遵循的規則(比如標簽怎麽寫等等)。
參考文獻
https://blog.csdn.net/java_xuetu/article/details/80064019
http://www.w3school.com.cn/xml/xml_namespaces.asp
https://www.cnblogs.com/gonjan-blog/p/6637106.html
spring16-----XML命名空間和Spring配置文件中的頭