1. 程式人生 > >關於Spring配置檔案xml文件的schema約束

關於Spring配置檔案xml文件的schema約束

最開始使用Spring框架的時候,對於其配置檔案xml,只是網上得知其使用方法,而不明其意。最近想著尋根問底的探究一下。以下是本文主要內容:

1、配置檔案示例。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"      
	xmlns:mvc="http://www.springframework.org/schema/mvc"   
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	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  
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context.xsd  
            http://www.springframework.org/schema/mvc  
            http://www.springframework.org/schema/mvc/spring-mvc.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 "
	default-autowire="byName">
</beans>
上面是我們最常用的applicationContext.xml的配置檔案的最開始部分,很多時候我們只是知道這些必須新增,但為什麼,新增哪些都不甚清楚。我們知道spring在啟動的時候會驗證 xml文件,這些引入的schema即用來驗證配置檔案的xml文件語法的正確性。下面先來看看如何驗證xml文件驗證的相關知識。

2、xml的schema約束

先來說說xml文件的schema約束,它定義了xml文件的結構,內容和語法,包括元素和屬性、關係的結構以及資料型別等等。有以下幾點需要遵循:

1) 、所有的標籤和屬性都需要Schema來定義。(schema本身由w3c來定義)。

2)、所有的schema檔案都需要以個ID,這裡我們稱之為 namespace,其值時一個url,通常是這個xml的xsd檔案的地址。

3)、namespace值由 targetNamespace屬性來指定

4)、引入一個schema約束,使用屬性xmlns,屬性值即為對應schema檔案的名稱空間 nameSpace。

5)、如果引入的schema非w3c組織定義的,必須指定schema檔案的位置,schema檔案的位置由 schemaLocation來指定。

6)、引入多個schema檔案需要使用 別名。別名形式如下: xmlns:alias。

3、對於上述配置檔案的詳細解讀

1)、宣告預設的名稱空間(xmlns="http://www.springframework.org/schema/beans")

2)、宣告XML Schema例項名稱空間(http://www.w3.org/2001/XMLSchema-instance),並將xsi字首與該名稱空間繫結,這樣模式處理器就可以識別xsi:schemaLocation屬性。XML Schema例項名稱空間的字首通常使用xsi

3)、 使用xsi:schemaLocation屬性指定名稱空間(http://www.springframework.org/schema/beans)和模式位置(http://www.springframework.org/schema/beans/spring-beans.xsd)相關。XML Schema推薦標準中指出,xsi:schemaLocation屬性可以在例項中的任何元素上使用,而不一定是根元素,不過,xsi:schemaLocation屬性必須出現在它要驗證的任何元素和屬性之前。

4)、使用別名引入多個schema檔案。如上述 xmlns:mvc 、xmlns:tx 、xmlns:context等等。

4、使用帶版本號的xsd檔案有何弊端?

首先來看看Spring是如何驗證xml文件的。Spring預設在啟動時是要載入XSD檔案來驗證xml檔案的,所以如果有的時候斷網了,或者一些開源軟體切換域名,那麼就很容易碰到應用啟動不了,為了防止這種情況,Spring提供了一種機制,預設從本地載入XSD檔案。開啟spring-context-4.1.2.RELEASE.jar,可以看到裡面有兩個特別的檔案:
spring.handlers如下:
http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler
http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler
http\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler
http\://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler
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.xsd=org/springframework/context/config/spring-context-4.1.xsd
http\://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd
http\://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd
http\://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd
http\://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd
http\://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd
http\://www.springframework.org/schema/jee/spring-jee-4.0.xsd=org/springframework/ejb/config/spring-jee-4.0.xsd
http\://www.springframework.org/schema/jee/spring-jee-4.1.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd
http\://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd
http\://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd
http\://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd
http\://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd
http\://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd
http\://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd
http\://www.springframework.org/schema/lang/spring-lang-4.0.xsd=org/springframework/scripting/config/spring-lang-4.0.xsd
http\://www.springframework.org/schema/lang/spring-lang-4.1.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd
http\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd
http\://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd
http\://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd
http\://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd
http\://www.springframework.org/schema/task/spring-task-4.0.xsd=org/springframework/scheduling/config/spring-task-4.0.xsd
http\://www.springframework.org/schema/task/spring-task-4.1.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd
http\://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd
http\://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd
http\://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd
http\://www.springframework.org/schema/cache/spring-cache-4.0.xsd=org/springframework/cache/config/spring-cache-4.0.xsd
http\://www.springframework.org/schema/cache/spring-cache-4.1.xsd=org/springframework/cache/config/spring-cache-4.1.xsd
http\://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-4.1.xsd
再開啟jar包裡的org/springframework/context/config/ 目錄,可以看到下面有 很明顯,可以想到Spring是把XSD檔案放到本地了,再在spring.schemas裡做了一個對映,優先從本地裡載入XSD檔案。並且Spring很貼心,把舊版本的XSD檔案也全放了。這樣可以防止升級了Spring版本,而配置檔案裡用的還是舊版本的XSD檔案,然後斷網了,應用啟動不了。 從上述xml文件中可以看出,當沒有配置版本號時,使用的即當前版本的xml schema文件。
http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.1.xsd
上述即為對spring配置文件中的xml 文件驗證的一部分說明,也是我在學習過程中所忽略的部分。文中有參考一些視訊教程還有一篇博文 為什麼在Spring的配置裡,最好不要配置xsd檔案的版本號


相關推薦

關於Spring配置檔案xmlschema約束

最開始使用Spring框架的時候,對於其配置檔案xml,只是網上得知其使用方法,而不明其意。最近想著尋根問底的探究一下。以下是本文主要內容: 1、配置檔案示例。 <?xml version="1.0" encoding="UTF-8"?> <beans x

Spring配置檔案xml約束出錯

Multiple annotations found at this line:- cvc-elt.1: Cannot find the declaration of element 'beans'.

Spring】application.xml配置

framework username beans factory 管理器 ext ram nco classname 什麽是Spring? Spring是分層的javaEE full-stack(一站式)輕量級開源框架。 ---註解配置--針對SSM <?xm

spring中applicationContex.xml

span -name start resource oca default spa 文件 mapping <mvc:resources mapping="/js/**" location="/WEB_INF/js"/>主要用於靜態資源的訪問. 使用springM

JAVA程序操作hbase的Maven配置pom.xml

pom hba title mave name rep word tle 使用 軟件151 盧煒傑 由於工作需要,用到Java連接Hbase讀寫數據。原來的方式是直接將依賴的jar包全部下載下來,然後工程裏面引用。 現想重構下,改為使用Maven管理,遇到了各種坑,各

數據源連接數據庫配置相關xml

servle b- 內容 筆記 icu web-inf tom ica 得出 學完數據源連接數據後,做個筆記,當我們的程序對數據庫訪問頻繁時,為了提高程序運行效率,我們可以通過 數據源連接數據庫,從數據庫連接池中直接取得出於空閑狀態的數據庫連接對象,以下是相關xml文件的配

轉】Eclipse編輯Spring配置檔案xml時自動提示類class包名

轉自  http://bigdragon.iteye.com/blog/2396081   第一步,先檢視下自己的Eclipse是什麼版本,步驟如下: 1.1 點選Eclipse選單‘Help  -> About Eclipse’,

Eclipse編輯Spring配置檔案xml時自動提示類class包名

以Spring2.0為例: 下載地址: Csdn: http://download.csdn.net/download/hh775313602/9812757 沒積分的可以去百度網盤下載,我已共享: http://pan.baidu.com/s/1nv9bb1f

Spring(十二)使用Springxml配置方式實現AOP

註解 asp odi ns2 package hit 實現 object space 配置文件與註解方式的有非常大不同,多了非常多配置項。 beans2.xml <?xml version="1.0" encoding="UT

Spring介紹及配置(XML配置和註解配置)

處理 tis 配置文件 3.0 span 特點 inverse logging 結構 本節內容: Spring介紹 Spring搭建 Spring概念 Spring配置講解 使用註解配置Spring 一、Spring介紹 1. 什麽是Spring

認識配置schema.xml(managed-schema

第一步 xpl border 內容 IV uek plain sta nag 1、schema文件是在SolrConfig中的架構工廠定義,有兩種定義模式: 1.1、默認的托管模式: solr默認使用的就是托管模式。也就是當在solrconfig.xml文件中沒有顯

引用多個schema約束來建立xml

<?xml version="1.0" encoding="UTF-8"?> <w3:schema xmlns:w3="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.examp

Spring配置檔案xml中報錯:xml檔案報錯Referenced file contains errors (http://www.springframework.org/schema/cont

今天開啟我的ssh專案,突然Spring的xml配置檔案中報錯: Referenced file contains errors (http://www.springframework.org/s

XMLSchema.xsd 檔案約束xml格式內容

XMLSchema.xsd 檔案,用來約束xml內容格式的。<?xml version="1.0" encoding="utf-8"?> <xs:schema id="XMLSchemaLettle" targetNamespace="http:/

Spring配置檔案schema約束

   [html] view plain copy print?http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context.xsd

Spring通過註解配置bean的xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/b

struts.xml中action配置、OGNL的投影映射、OGNL表達式的符號

傳參 路徑 req rop [0 path 中一 prop 後綴 在struts.xml文件中不同的action配置,請求的路徑是不一樣的 1、請求 path = user!query.action; 配置如下; <action name="user" class=

SpringMVC xml配置

load pla 無法顯示 encoding 控制器 work -- 1.0 charset 在這裏要特別的註意路徑的問題<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://ww

mybatis mapper xml配置resultmap時,id行和result行有什麽區別?

什麽 column invoice 配置 app nbsp ava customer entity mybatis mapper xml文件配置resultmap時,id行和result行有什麽區別? <resultMap id = "CashInvoiceMap"

duilib中加入自己定義控之後怎麽可以在xml配置使用

調用 tin 不同 center ger 使用 我們 article virtual 加入自己定義控件可能有兩種不同的情況: 1、 在duilib庫中加入的自己定義控件。 2、 在我們的應用程序中自己重寫了一個控件。 以下開始解說不同的情況下怎麽才幹支持在