转载地址:https://www.cnblogs.com/javastack/p/13952036.html

1 为什么 dubbo 启动没有问题?

这篇 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">

咱们也能够在前面加上版本号:

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

2 XML的一些概念

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

xmlschema 里有 namespace,能够给它起个别名。比方常见的 springnamespace

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

通常状况下,namespace对应的 URI 是一个寄存 XSD 的地址,只管标准没有这么要求。

如果没有提供 schemaLocation ,那么 Springxml 解析器会从 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 前面配置的字符串都是成对的,后面的是 namespaceURI,前面是 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.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.xsdspring-context-3.0.xsdspring-context-3.1.xsdspring-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

同样,咱们关上 dubbojar 包,能够在它的 spring.schemas 文件里看到有这样的配置:

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

所以,Spring 在加载 dubbo 时,会从 dubbo 的 jar 里加载 dubbo.xsd。

3 如何跳过Spring的XML校验?

能够用这样的形式来跳过校验:

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

4 如何写一个本人的spring xml namespace扩大?

能够参考 Spring 的文档,实际上是相当简略的。只有实现本人的 NamespaceHandler,再配置一下 spring.handlersspring.schemas 就能够了。

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

5 其它的一些东东

避免 XSD 加载不胜利的一个思路:http://hellojava.info/?p=135

齐全的 Springnamespace 的列表

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

Spring core    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 - UtilNamespaceHandlerSpring Security    security - SecurityNamespaceHandler    oauth - OAuthSecurityNamespaceHandlerSpring integration    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

6 总结

为什么不要在 Spring 的配置里,配置上 XSD 的版本号?

因为如果没有配置版本号,取的就是以后 jar 里的 XSD 文件,缩小了各种危险。 而且这样约定大于配置的形式很优雅。

参考:

  • http://stackoverflow.com/ques...
  • http://stackoverflow.com/ques...
  • http://docs.spring.io/spring/...