spring配置文件頭部xmlns配置精髓

<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項目時就頭疼,經過網絡各類複製別人的配置文件,而後一不當心就報錯了,因此今天想探探究竟。java

  如上是一個spring-context配置文件spring

xmlns部分:spring-mvc

1.網絡

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  這個是每一個配置文件必須的部分,也就是spring的根本。mvc

  聲明xml文件默認的命名空間,表示未使用其餘命名空間的全部標籤的默認命名空間。spa

  聲明XML Schema 實例,聲明後就可使用 schemaLocation 屬性了。code

2.xml

xmlns:aop="http://www.springframework.org/schema/aop"

  這個就是spring配置文件裏面須要使用到aop的標籤,聲明前綴爲aop的命名空間,後面的URL用於標示命名空間的地址不會被解析器用於查找信息。其唯一的做用是賦予命名空間一個唯一的名稱。當命名空間被定義在元素的開始標籤中時,全部帶有相同前綴的子元素都會與同一個命名空間相關聯。而後其餘好比context(針對組件標籤)、MVC(針對mvc標籤)、tx(針對事務標籤)都同樣的意思。blog

 

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規範文件。

相關文章
相關標籤/搜索