閱讀668 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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

為什麼dubbo啟動沒有問題?

這篇blog源於一個疑問:

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

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="https://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="https://www.springframework.org/schema/beans
	        https://www.springframework.org/schema/beans/spring-beans.xsd
	        https://code.alibabatech.com/schema/dubbo
	        https://code.alibabatech.com/schema/dubbo/dubbo.xsd">
我們都知道Spring在啟動時是要檢驗XML文件的。或者為什麼在Eclipse裏xml沒有錯誤提示?

比如這樣的一個Spring配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
我們也可以在後麵加上版本號:

xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
有這個版本號和沒有有什麼區別呢?

XML的一些概念

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

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

	xmlns:mvc="https://www.springframework.org/schema/mvc"
	xmlns:context="https://www.springframework.org/schema/context"
通常情況下,namespace對應的URI是一個存放XSD的地址,盡管規範沒有這麼要求。如果沒有提供schemaLocation,那麼Spring的XML解析器會從namespace的URI裏加載XSD文件。我們可以把配置文件改成這個樣子,也是可以正常工作的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans/spring-beans.xsd"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">
schemaLocation提供了一個xml namespace到對應的XSD文件的一個映射,所以我們可以看到,在xsi:schemaLocation後麵配置的字符串都是成對的,前麵的是namespace的URI,後麵是xsd文件的URI。比如:

	xsi:schemaLocation="https://www.springframework.org/schema/beans
	https://www.springframework.org/schema/beans/spring-beans.xsd
	https://www.springframework.org/schema/security
	https://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就可以了。

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

其它的一些東東

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

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

齊全的Spring的namespace的列表

https://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文件,減少了各種風險。
而且這樣約定大於配置的方式很優雅。

參考:

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

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

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

最後更新:2017-04-03 12:55:58

  上一篇:go android來電歸屬地提醒
  下一篇:go flappy bird遊戲源代碼揭秘和下載