為什么在Spring的配置里,最好不要配置xsd文件的版本號


為什么dubbo啟動沒有問題?

原文鏈接:http://www.tuicool.com/articles/YRn67zM

這篇blog源於一個疑問:

我們公司使了阿里的dubbo,但是阿里的開源網站http://code.alibabatech.com,掛掉有好幾個月了,為什么我們的應用啟動沒有問題?

我們的應用的Spring配置文件里有類似的配置:

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"  xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd  http://code.alibabatech.com/schema/dubbo  http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

我們都知道Spring在啟動時是要檢驗XML文件的。或者為什么在Eclipse里xml沒有錯誤提示? 

比如這樣的一個Spring配置:

<?xml version="1.0" encoding="UTF-8"?>
<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"> </beans>

我們也可以在后面加上版本號:

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

有這個版本號和沒有有什么區別呢?

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

Spring是如何校驗XML的

Spring默認在啟動時是要加載XSD文件來驗證xml文件的,所以如果有的時候斷網了,或者一些開源軟件切換域名,那么就很容易碰到應用啟動不了。我記得當時Oracle收購Sun公司時,遇到過這個情況。

為了防止這種情況,Spring提供了一種機制,默認從本地加載XSD文件。打開spring-context-3.2.0.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.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

同樣,我們打開dubbo的jar包,可以在它的spring.schemas文件里看到有這樣的配置:

http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd

所以,Spring在加載dubbo時,會從dubbo的jar里加載dubbo.xsd。 

如何跳過Spring的XML校驗?

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

GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.setValidating(false);

如何寫一個自己的spring xml namespace擴展

可以參考Spring的文檔,實際上是相當簡單的。只要實現自己的NamespaceHandler,再配置一下spring.handlers和spring.schemas就可以了。

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/extensible-xml.html

其它的一些東東

防止XSD加載不成功的一個思路

http://hellojava.info/?p=135

齊全的Spring的namespace的列表

http://stackoverflow.com/questions/11174286/spring-xml-namespaces-how-do-i-find-what-are-the-implementing-classes-behind-t 

Spring core

Spring Security

Spring integration

總結:

為什么不要在Spring的配置里,配置上XSD的版本號? 
因為如果沒有配置版本號,取的就是當前jar里的XSD文件,減少了各種風險。 
而且 這樣約定大於配置的方式很優雅。

參考:

http://stackoverflow.com/questions/10768873/spring-di-applicationcontext-xml-how-exactly-is-xsischemalocation-used 

http://stackoverflow.com/questions/11174286/spring-xml-namespaces-how-do-i-find-what-are-the-implementing-classes-behind-t 

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/extensible-xml.html 


免責聲明!

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



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