Maven编译,dubbo.xsd问题解决办法

当项目使用dubbo服务,Maven编译时可能出现找不到dubbo.xsd的错误。原因是网络上无法访问阿里巴巴的dubbo xsd实例。解决方案包括:1) 在Eclipse中将引用指向本地xsd文件;2) 将xsd文件放入工程classpath并修改引用路径。由于项目使用jenkins自动编译,选择了第二种方法。但需要注意,此改动可能导致Spring NamespaceHandler无法定位,需要在pom文件中显式指定dubbo相关依赖。

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>

上面是需要增加的片段。
以上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值