spring配置文件中xsd引用問題

下載spring-framework-5.1.0.RELEASE-dist.zip包,


解壓後,在目錄 spring-framework-5.1.0.RELEASE\libs\ 下找到 spring-context-5.1.0.RELEASE.jar 包:


用壓縮軟件解壓該包後,在目錄 spring-context-5.1.0.RELEASE\META-INF 下找到 spring.schemas 文件:


用Sublime打開該文件便可看到所有可用的 xsd 版本號,實際項目的 xml 文件中可以不寫版本號,會自動引用最新版本


src根目錄下的xml文件的<beans>標籤屬性示例如下:

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

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"


轉載:

https://blog.csdn.net/dingqinghu/article/details/46758671 

XML的一些概念

首先來看下xml的一些概念:

xml的schema裏有namespace,可以給它起個別名。比如常見的spring的namespace:


xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"

通常情況下,namespace對應的URI是一個存放XSD的地址,儘管規範沒有這麼要求。如果沒有提供schemaLocation,那麼Spring的XML解析器會從namespace的URI里加載XSD文件。我們可以把配置文件改成這個樣子,也是可以正常工作的:



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

schemaLocation提供了一個xml namespace到對應的XSD文件的一個映射,所以我們可以看到,在xsi:schemaLocation後面配置的字符串都是成對的,前面的是namespace的URI,後面是xsd文件的URI。比如:



xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security.xsd"



Spring是如何校驗XML的

Spring默認在啓動時是要加載XSD文件來驗證xml文件的,所以如果有的時候斷網了,或者一些開源軟件切換域名,那麼就很容易碰到應用啓動不了。

爲了防止這種情況,Spring提供了一種機制,默認從本地加載XSD文件。打開spring-context-3.2.0.RELEASE.jar,可以看到裏面有兩個特別的文件:

spring.handlers


http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandlerhttp\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandlerhttp\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandlerhttp\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandlerhttp\://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.xsdhttp\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsdhttp\://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsdhttp\://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsdhttp\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.2.xsd...



再打開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是把XSD文件放到本地了,再在spring.schemas裏做了一個映射,優先從本地裏加載XSD文件。

並且Spring很貼心,把舊版本的XSD文件也全放了。這樣可以防止升級了Spring版本,而配置文件裏用的還是舊版本的XSD文件,然後斷網了,應用啓動不了。

我們還可以看到,在沒有配置版本號時,用的就是當前版本的XSD文件:


http\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.2.xsd


如何跳過Spring的XML校驗?


可以用這樣的方式來跳過校驗:


GenericXmlApplicationContext context = new GenericXmlApplicationContext();context.setValidating(false);
</pre><pre code_snippet_id="264875" snippet_file_name="blog_20140330_11_9075882" name="code" class="html" style="border: 1px solid rgb(255, 255, 204); overflow: auto;">


這樣可以解決在斷網情況下加載xsd文件連接超時的問題。
schema_reference.4:無法讀取方案文檔 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd,原因是1)無法找到文檔 2)無法讀取文檔 3)文檔的根元素不是<xsd:schema>.
caused by java.net.ConnectionException:Connection timed out:connect.
...
原因就是在spring的jar包中META-INF/spring.schemas中沒有加上
http\://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
http\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
沒有加上這個,則xsd加載時就會去網上找,在無網情況下就會報錯。
另外,使用eclipse的導出爲可運行jar包時,多個spring jar包同時有,spring.handlers、spring.schemas、spring.tooling三個文
件,但是打成jar包時在META-INF/下只能留一份,這時需要將多個文件中的內容合併在一個文件中。

--------------------- 作者:dingqinghu  來源:CSDN  原文:https://blog.csdn.net/dingqinghu/article/details/46758671?utm_source=copy  版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章