關於spring配置文件的頭部編寫


//普通spring配置文件模板
1
<?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://www.springframework.org/schema/beans" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 6 7 </beans>

 

 1 //添加注解后的模板格式
 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:tx="http://www.springframework.org/schema/tx" 
 6     xmlns:context="http://www.springframework.org/schema/context"
 7        xmlns:mvc="http://www.springframework.org/schema/mvc" 
 8     xmlns:task="http://www.springframework.org/schema/task"
 9        xsi:schemaLocation="
10         http://www.springframework.org/schema/beans
11         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
12         http://www.springframework.org/schema/tx
13         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
14         http://www.springframework.org/schema/aop
15         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
16         http://www.springframework.org/schema/context
17         http://www.springframework.org/schema/context/spring-context-3.1.xsd
18         http://www.springframework.org/schema/mvc
19         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
20         http://www.springframework.org/schema/task
21         http://www.springframework.org/schema/task/spring-task-3.1.xsd">

 

1 xmlns="http://www.springframework.org/schema/beans"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
這是spring配置文件必須的部分。
第1行:聲明xml文件默認的命名空間,表示未使用其他命名空間的所有標簽的默認命名空間。<bean id="" ></bean>默認命名空間,不需要加前綴<bean:bean id="">.
第2行:聲明XML Schema 實例,聲明后就可以使用 schemaLocation 屬性了。
1 xmlns:aop="http://www.springframework.org/schema/aop"

這個就是spring配置文件里面需要使用到aop的標簽,聲明前綴為aop的命名空間,后面的URL用於標示命名空間的地址不會被解析器用於查找信息。其惟一的作用是賦予命名空間一個惟一的名稱。當命名空間被定義在元素的開始標簽中時,所有帶有相同前綴的子元素都會與同一個命名空間相關聯。然后其他比如context(針對組件標簽)、MVC(針對mvc標簽)、tx(針對事務標簽)都一樣的意思。

1 xsi:schemaLaction部分:
2 http://www.springframework.org/schema/beans
3 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

是為上面配置的命名空間指定xsd規范文件,這樣你在進行下面具體配置的時候就會根據這些xsd規范文件給出相應的提示,比如說每個標簽是怎么寫的,都有些什么屬性是都可以智能提示的,以防配置中出錯而不太容易排查,在啟動服務的時候也會根據xsd規范對配置進行校驗。但是這里需要為你上面xmlns里面配置的mvc、aop、tx等都配置上xsd規范文件。

 

------------------------------------------------------------------------------------------------------

【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文件,從而實現標簽的提示。

總結:

XML Schema命名空間避免命名沖突,就像Java中的package一樣,將不同作用的標簽分門別類(比如Spring中的tx命名空間針對事務類的標簽,context命名空間針對組件的標簽)。

參考:

https://www.cnblogs.com/EasonJim/p/6880329.html

 


免責聲明!

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



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