比如一個標准的beans.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
解釋:
1、【xmlns="http://www.springframework.org/schema/beans"】
聲明xml文件默認的命名空間,表示未使用其他命名空間的所有標簽的默認命名空間。
2、【xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"】
聲明XML Schema實例,聲明后就可以使用schemaLocation屬性。
3、【xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd】
指定Schema的位置這個屬性必須結合命名空間使用。這個屬性有兩個值,第一個值表示需要使用的命名空間。第二個值表示供命名空間使用的XML schema的位置。
上面配置的命名空間指定xsd規范文件,這樣你在進行下面具體配置的時候就會根據這些xsd規范文件給出相應的提示,比如說每個標簽是怎么寫的,都有些什么屬性是都可以智能提示的,在啟動服務的時候也會根據xsd規范對配置進行校驗。
配置技巧:對於屬性值的寫法是有規律的,中間使用空格隔開,后面的值是前面的補充,也就是說,前面的值是去除了xsd文件后得來的。
疑問:
對於一些配置文件上xsi:schemaLocation的屬性是這樣寫的:
【xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd】
指定了具體版本,這個是什么意思?那么下次如何快速的知道是哪個版本?
解釋:
在每個xsi:schemaLocation屬性中,都是一個具有指定版本號的xsd文件,而對於沒有加版本號的問題,是由於使用了默認值,實質上這個默認值是有一個具體的版本號的。當然,你也可以使用具體的版本號。
而對於這些xsd文件的路徑查找的方法,可以定位到每一個jar包去找,比如上面使用了4.1.4版本beans的jar包,那么可以通過Eclipse打開spring-beans-4.1.4.RELEASE.jar文件,並打開META-INF/spring.schemas文件,如下所示:
可以看出,默認的地址上也是制定了具體的版本號的,那么根據后面的地址打開org/springframework/beans/factory/xml目錄,如下所示:
可以看出最后會來這里定位xsd文件,從而實現標簽的提示。
而這里只展示了要用的部分的url,一般這些都是根據自己的功能需要引入的,比如我要引入util的功能,那么url也可以通過這樣的方法快速的找到這些地址。
注意:xsi:schemaLocation屬性可以指定多個,指定多個的意思就是引入多個功能。
如下所示是一個引入了使用aop和tx功能的beans.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> ... </beans>
解釋:
1、【xmlns:aop="http://www.springframework.org/schema/aop"】
聲明前綴為aop的命名空間,后面的URL用於標示命名空間的地址不會被解析器用於查找信息。其惟一的作用是賦予命名空間一個惟一的名稱。當命名空間被定義在元素的開始標簽中時,所有帶有相同前綴的子元素都會與同一個命名空間相關聯。
同時也是為beans.xml文件引入aop的功能,引入后就可以在包裹的范圍內使用<aop>標簽。
2、【xmlns:tx="http://www.springframework.org/schema/tx"】
原理同上。
3、xsi:schemaLocation屬性同上解釋,使用快速的方法來定位和編寫這個url。
一些技巧:
關於在beans.xml要使用哪些功能,官網上已經提供了每個功能說明和標准的頭文件信息,當我們在開發使用時要哪些功能,都可以上官網去定位。
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#xsd-configuration
其實這個地址的入口在這里http://projects.spring.io/spring-framework/,打開之后,定位到右邊邊,想要哪個版本點進去即可。
關於書寫的技巧,比如一些標簽已經沒有內嵌了,那么會有兩種寫法,這兩種寫法效果都是一直的,如:<context:annotation-config/>和<context:annotation-config></context:annotation-config>,為了美觀來說,我們會選擇前者。
總結:
XML Schema命名空間避免命名沖突,就像Java中的package一樣,將不同作用的標簽分門別類(比如Spring中的tx命名空間針對事務類的標簽,context命名空間針對組件的標簽)。
參考:
http://iswift.iteye.com/blog/1657537
http://www.oschina.net/question/107380_111872?sort=time
http://blog.csdn.net/f_639584391/article/details/50167321