spring applicationContext.xml 文件頭的配置說明

環境及方法

spring 4.2.7
使用log4j 記得打開debug模式.而且自己把java類及方法多少行都打印出來

頭文件舉例

<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"    
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

xmlns:xxx 後面的是命名空間
xsi:schemaLocation 中每一個命名空間對應一個xsd文件.
xmlns=”http://www.springframework.org/schema/beans” 默認命名空間

使用不同的標籤要配置spring不同的組件

比如

對應
xmlns:context=”http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
但是程序是如何尋找xsd.來驗證xml中自己配置的合法性.已經xsd的版本如何選擇.

尋找

就以context爲例.我們尋找到spring-context-4.2.7.Release.jar找到
org.springframework.context.config包下發現.
spring-context-2.5.xsd
spring-context-3.0.xsd
spring-context-3.1.xsd
spring-context-3.2.xsd
spring-context-4.0.xsd
spring-context-4.1.xsd
spring-context-4.2.xsd
這裏面能夠找到一個與上面配置一樣的xsd文件.但是這麼多,spring爲什麼要保留這麼多.而不用一個最新的就可以了.據說是因爲.當spring版本升級的時候,可以不用修改xml文件(到底是不是這個原因,還真不知道.)
但是大家又看到了,我自己每次配置去尋找這個.xsd嗎?而且我自己現在的版本是4.2.7.但是並沒有.
有人可能要說,人家取前兩個數字的版本.但是我記得spring3.5這個版本是有的.並沒有發現3.5版本的xsd
那麼到底我們配置的時候要怎麼配置.

不配置版本號

經研究發現.配置還有別的地方.而且我們配置的時候網絡路徑.如果連接不上網.那豈不是很尷尬.
經過查看spring啓動的log發現.

Loading schema mappings from [META-INF/spring.schemas]
Loaded schema mappings:{http://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd, http://www.springframework.org/schema/cache/spring-cache-4.2.xsd=org/springframework/cache/config/spring-cache-4.2.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/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd=org/springframework/jdbc/config/spring-jdbc-4.1.xsd, http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd=org/springframework/web/servlet/config/spring-mvc-4.1.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, 
.....................後面太多,我省略掉了

很明顯,我們雖然配置的是網絡路徑,但是log中有一個mapping可以去本地的classpath中尋找.
但是還有一個問題.我們還是不知道如何配置版本.
可以去百度搜索(spring xsd文件最好不要配置版本)
那麼到底是爲什麼那?
上面我省略的太多.我們可以看一個jar下的spring.schemas文件.

{http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd
http\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd
http\://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd
http\://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd
http\://www.springframework.org/schema/context/spring-context-4.0.xsd=org/springframework/context/config/spring-context-4.0.xsd
http\://www.springframework.org/schema/context/spring-context-4.1.xsd=org/springframework/context/config/spring-context-4.1.xsd
http\://www.springframework.org/schema/context/spring-context-4.2.xsd=org/springframework/context/config/spring-context-4.2.xsd
http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.2.xsd
此處仍然省略一些.
}

我們看到當我們不配置版本號的xsd的時候.那麼會去自動尋找jar最新版本的xsd文件.
好了,到此大家明白了spring xml文件頭文件的設置
怎麼配置,以及到底要不要版本號的問題.

給出正確的配置版本

<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"
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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

當然spring2.0之前還在用dtd文件不是這個原理.

新的發現

當在applicationContext.xml import其他xml文件的時候.比如

下面是文件頭

<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns="http://www.springframework.org/schema/security"
xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/security      http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

會發現.文件的根節點不在是 而是
而且xmlns發生了變化.也就是說這個xml的默認命名空間已經不是在beans的按個uri了.而是
http://www.springframework.org/schema/security
而要想像applicationContext.xml中增加一個bean的話.
則變成了這樣.

<b:bean id="defaultAccessDeniedHandler" class="com.renderbus.cm.security.DefaultAccessDeniedHandler"></b:bean>
發佈了161 篇原創文章 · 獲贊 18 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章