作者:橫云斷嶺
https://blog.csdn.net/hengyunabc/article/details/22295749

為什么dubbo啟動沒有問題?
這篇blog源于一個疑問:
我們公司使了阿里的dubbo,掛掉有好幾個月了,為什么我們的應用啟動沒有問題?我們的應用的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">
我們也可以在后面加上版本號:
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
-
aop-AopNamespaceHandler -
c-SimpleConstructorNamespaceHandler -
cache-CacheNamespaceHandler -
context-ContextNamespaceHandler -
jdbc-JdbcNamespaceHandler -
jee-JeeNamespaceHandler -
jms-JmsNamespaceHandler -
lang-LangNamespaceHandler -
mvc-MvcNamespaceHandler -
oxm-OxmNamespaceHandler -
p-SimplePropertyNamespaceHandler -
task-TaskNamespaceHandler -
tx-TxNamespaceHandler -
util-UtilNamespaceHandler -
security-SecurityNamespaceHandler -
oauth-OAuthSecurityNamespaceHandler -
int-IntegrationNamespaceHandler -
amqp-AmqpNamespaceHandler -
event-EventNamespaceHandler -
feed-FeedNamespaceHandler -
file-FileNamespaceHandler -
ftp-FtpNamespaceHandler -
gemfire-GemfireIntegrationNamespaceHandler -
groovy-GroovyNamespaceHandler -
http-HttpNamespaceHandler -
ip-IpNamespaceHandler -
jdbc-JdbcNamespaceHandler -
jms-JmsNamespaceHandler -
jmx-JmxNamespaceHandler -
mail-MailNamespaceHandler -
redis-RedisNamespaceHandler -
rmi-RmiNamespaceHandler -
script-ScriptNamespaceHandler -
security-IntegrationSecurityNamespaceHandler -
sftp-SftpNamespaceHandler -
stream-StreamNamespaceHandler -
twitter-TwitterNamespaceHandler -
ws - WsNamespaceHandler
-
xml-IntegrationXmlNamespaceHandler -
xmpp - XmppNamespaceHandler
總結:
為什么不要在Spring的配置里,配置上XSD的版本號?
因為如果沒有配置版本號,取的就是當前jar里的XSD檔案,減少了各種風險,
而且這樣約定大于配置的方式很優雅,
近期熱文推薦:
1.Java 15 正式發布, 14 個新特性,重繪你的認知!!
2.終于靠開源專案弄到 IntelliJ IDEA 激活碼了,真香!
3.我用 Java 8 寫了一段邏輯,同事直呼看不懂,你試試看,,
4.吊打 Tomcat ,Undertow 性能很炸!!
5.《Java開發手冊(嵩山版)》最新發布,速速下載!
覺得不錯,別忘了隨手點贊+轉發哦!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/209246.html
標籤:其他
上一篇:git和pycharm連接
