<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" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">
使用spring已經好幾年了,但是每次遇到要自己配置spring項目時就頭疼,通過網絡各種復制別人的配置文件,然后一不小心就報錯了,所以今天想探探究竟。
如上是一個spring-context配置文件
xmlns部分:
1.
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
這個是每個配置文件必須的部分,也就是spring的根本。
聲明xml文件默認的命名空間,表示未使用其他命名空間的所有標簽的默認命名空間。
聲明XML Schema 實例,聲明后就可以使用 schemaLocation 屬性了。
2.
xmlns:aop="http://www.springframework.org/schema/aop"
這個就是spring配置文件里面需要使用到aop的標簽,聲明前綴為aop的命名空間,后面的URL用於標示命名空間的地址不會被解析器用於查找信息。其惟一的作用是賦予命名空間一個惟一的名稱。當命名空間被定義在元素的開始標簽中時,所有帶有相同前綴的子元素都會與同一個命名空間相關聯。然后其他比如context(針對組件標簽)、MVC(針對mvc標簽)、tx(針對事務標簽)都一樣的意思。
xsi:schemaLaction部分:
1.
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
是為上面配置的命名空間指定xsd規范文件,這樣你在進行下面具體配置的時候就會根據這些xsd規范文件給出相應的提示,比如說每個標簽是怎么寫的,都有些什么屬性是都可以智能提示的,以防配置中出錯而不太容易排查,在啟動服務的時候也會根據xsd規范對配置進行校驗。但是這里需要為你上面xmlns里面配置的mvc、aop、tx等都配置上xsd規范文件。