Maven编译,dubbo.xsd问题的解决办法
错误现象
项目使用dubbo服务,在maven编译时,项目出现例如:
org.springframework.beans.factory.xml.XmlBeanDefinitionReader -
Ignored XML validation warning
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
或者
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:/beans/dubbo-consumer.xml]
Offending resource: class path resource [beans/applicationContext.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from class path resource [beans/dubbo-consumer.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 59; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'.
这样的错误信息,可能两者皆有。
错误原因
该错误是由于dubbo:reference的定义在网络上不存在,阿里关于dubbo的XMLschema的实例访问不了造成的,就是下面的这句话
<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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
最后一句的http://code.alibabatech.com/schema/dubbo/dubbo.xsd,访问不了造成的。
解决办法
解决该问题主要有两种办法。
1. eclipse中修改xml参照路径,索引到本地dubbo.xsd文件(有一定限制),网上查找的大部分为该方法。
略。请自行搜索。
2. 将dubbo.xsd文件编译在工程中(通用)
将dubbo.xsd保存在工程的classpath目录中,并且在dubbo的XMLschema的参照出,修改为参照项目的工程路径下文件,代码如下:
<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-2.5.xsd
http://code.alibabatech.com/schema/dubbo classpath:/dubbo.xsd">
本项目由于使用jenkins自动编译,无法通过本地eclipse导入的办法,智能采用办法2,这样其他人修改时也不受影响。唯一的问题是编译的内容略微增加,系统代码有一点点侵入。
注意事项
按照办法2修改时,系统会出现 Unable to locate Spring NamespaceHandler for XML schema namespace 的错误,如下
ERROR org.springframework.web.context.ContextLoader -
Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:/beans/dubbo-consumer.xml]
Offending resource: class path resource [beans/applicationContext.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://code.alibabatech.com/schema/dubbo]
Offending resource: class path resource [beans/dubbo-consumer.xml]
就是提示无法指向XMLschema namespace,出现该问题说明刚才的修改已经成功,但是本项目中参照dubbo服务失败,需要在pom文件中显示指定dubbo相关的参照。Provider提供的API中指定的dubbo服务无法使用在本项目中。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
上面是需要增加的片段。
以上。
当项目使用dubbo服务,Maven编译时可能出现找不到dubbo.xsd的错误。原因是网络上无法访问阿里巴巴的dubbo xsd实例。解决方案包括:1) 在Eclipse中将引用指向本地xsd文件;2) 将xsd文件放入工程classpath并修改引用路径。由于项目使用jenkins自动编译,选择了第二种方法。但需要注意,此改动可能导致Spring NamespaceHandler无法定位,需要在pom文件中显式指定dubbo相关依赖。

2万+

被折叠的 条评论
为什么被折叠?



