spring配置文件中xsd引用問題


今天在Spring配置文件中配置如下事務屬性時,提示<tx is not bound(不受約束的),估計是配置文件的xsd沒配置好。

復制代碼
<!-- 2.配置事務屬性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"></tx:method>
<tx:method name="*"></tx:method>
</tx:attributes>
</tx:advice>
復制代碼

 

在xsi:schemaLocation=中增加:

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

錯誤就消失了。

網上查閱了相關資料,現總結如下:

xml文檔要有格式,為了Spring的配置文件增加的節點(比如<tx:advice)能符合要求、合法,必須通過引入校驗該xml格式的文件,也就是xsd文件。Spring通過配置可以聯網引入xsd文件,也可以在斷網下使用本地xsd文件校驗xml文件。

xsi:schemaLocation 屬性提供一種方法來查找在 XML 實例文檔中定義的命名空間的 XML 架構定義。它的值是用空白分隔的統一資源標識符 (URI) 對的列表,其中的每一對 URI 都依次包含一個命名空間以及該命名空間的 XML 架構定義(通常為 .xsd 文件)的位置。

xsi:schemaLocation 提供了xml的namespace到對應的xsd文件的映射,從下列配置可以看出xsi:schemaLocation 里面配置的字符串都是成對的,前面是namespace的URI,后面是xsd文件的URI

復制代碼
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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 
復制代碼

Spring在啟動時默認要加載xsd文件來驗證XML文件的,在瀏覽器中輸入復制的xmlns:tx的鏈接,也就是http://www.springframework.org/schema/tx,可以看到如下頁面:

以上都是可選的tx的xsd版本,而xsi:schemaLocation的http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 在用瀏覽器訪問是該版本xsd對應內容的頁面:

 

 

箭頭指的就是對<tx:advice進行xml校驗的部分,從上面代碼看出,tx還可以添加<tx:annotation-driven,<tx:jta-transaction-manager 節點,這個與xml中的提示是一致的:

 

advice部分的具體代碼如下:

View Code

 

如果此時無法上網,就無法從該網站獲取到該xsd文件,就無法對xml文件進行校驗了,容易發生應用啟動不了的情況。為此我們可以加載本地的xsd文件,這樣斷網情況下也可以啟動應用了。舉例子來說,若tx的xsd文件沒有配置,則要進行如下步驟進行配置

1、點開項目的lib,找到spring-tx-4.1.4.RELEASE.jar,右鍵屬性-復制文件路徑(復制jar之前的路徑),在文件夾地址欄粘貼,回車打開;

 

2、將spring-tx-4.1.4.RELEASE.jar解壓到某個文件夾,有如下文件:

3、在org\springframework\transaction\config路徑下可以找到:

里面還有舊版本的xsd文件,這樣可以避免配置文件用的還是舊版本的xsd文件,而Spring是新版本情況下,斷網應用啟動不了的情況。Spring這個確實想得挺周到的。

4、將tx的某個版本的xsd文件拷貝到工程的source folder下,然后xml的配置改成如下形式:

復制代碼
xsi:schemaLocation="http://www.springframework.org/schema/beans 
        classpath:beans/spring-beans-4.1.xsd 
        http://www.springframework.org/schema/mvc classpath:mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/context classpath:context/spring-context-4.1.xsd http://www.springframework.org/schema/aop classpath:aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx classpath:tx/spring-tx-4.1.xsd ">
復制代碼

這樣就可以從本地獲取xsd文件,就算斷網也可以解析xml文件了。

 附一份常見的spring配置文件的文件頭:

復制代碼
------------------------------------3.0----------------------------
 
<?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> </beans>
復制代碼

 當然你也可以根據使用的Spring的相應jar包版本更改對應的版本,比如上面的3.0可以改成4.1也行

http://www.springframework.org/schema/  該網站是Spring所有可能用到的結點屬性的xsd列表,點擊相應選項即可查到對應的xsd版本與xsd的URI:


免責聲明!

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



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