org.xml.sax.SAXParseException: Failed to read schema document 的原因分析与解决方法

本文分析了在无网络服务器环境下部署Spring应用时遇到的XML Schema加载失败问题,并提供了使用Maven shade插件进行资源合并的具体解决方案。

现象: 

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema documen
t 'http://www.springframework.org/schema/beans/spring-beans-3.1.xsd', because 1)
 could not find the document; 2) the document could not be read; 3) the root ele
ment of the document is not <xsd:schema>.


分析: 
参考http://blog.csdn.net/bluishglc/article/details/7596118 
这篇文章分析的比较深入,具体原因为: 
根据xx.jar\META-INF\spring.schemas文件,先在本地查找相关xsd文件,如果本地没有,则通过网络去获取,实际上上面问题往往出现在没有网络的服务器上(本地正常,传到服务器上,而服务器一般是不能访问外网的) 
但该文章并没有讲清楚更具体的处理措施,只是说了最好用maven的shade打包插件而不是assembly 


处理: 

必须在 <transformers> 里面加上下面内容 


<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
    <resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
    <resource>META-INF/spring.schemas</resource>

</transformer>


才会出现“shade能够将所有jar里的spring.schemas文件进行合并,在最终生成的单一jar包里,spring.schemas包含了所有出现过的版本的集合!”的效果 


参考 
http://blog.csdn.net/bluishglc/article/details/7596118 
http://mapserver000-gmail-com.iteye.com/blog/1182499

http://shuhucy.iteye.com/blog/1771684

http://stackoverflow.com/questions/8523997/unable-to-locate-spring-namespacehandler-for-xml-schema-namespace-http-www-sp/8574629#8574629

http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html

http://maven.apache.org/plugins/maven-shade-plugin/ 

转载于:https://www.cnblogs.com/leonxyzh/p/7289081.html

Spring框架在加载XML配置文件时,如果遇到`BeanDefinitionStoreException`和`SAXParseException`,通常表明配置文件的结构或内容存在XML Schema验证问题。以下是针对您提到的具体错误“`Document root element "beans", must match DOCTYPE root "null"`”的分析解决方案。 ### 错误原因分析 该错误表明XML文档的根元素`<beans>`预期的DOCTYPE声明不匹配。在Spring的XML配置中,如果使用了XSD模式(而不是旧的DTD),则不应包含DOCTYPE声明。然而,如果XML文件中包含了DOCTYPE声明,但其根元素为`<beans>`,而DOCTYPE声明的根元素不匹配,就会导致此错误。 具体来说,以下情况可能导致该问题: 1. **DOCTYPE声明实际根元素不匹配**:例如,声明了DOCTYPE为`web-app`,但实际根元素是`beans`。 2. **版本不兼容或错误的XML头信息**:例如,错误地混合使用了不同版本的Spring Schema或不完整的命名空间声明。 3. **XML格式错误**:例如,标签未正确闭合、属性拼写错误等。 ### 解决方案 #### 1. 移除DOCTYPE声明 如果使用的是基于XSD的Spring配置(推荐方式),则应完全移除XML文件中的DOCTYPE声明。Spring 2.0及以上版本主要使用XSD,而非旧的DTD方式。因此,确保XML文件的开头如下所示: ```xml <?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"> ``` #### 2. 确保`xsi:schemaLocation`正确 确保`xsi:schemaLocation`属性指向正确的Spring XSD文件。例如,对于Spring 5.x版本,应使用: ```xml xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" ``` 避免使用过时的版本号(如`spring-beans-4.0.xsd`),除非明确需要兼容旧版本。 #### 3. 检查XML结构完整性 确保XML文件的结构正确,包括: - 所有标签正确闭合。 - 属性值使用双引号。 - 没有拼写错误的标签或属性。 例如,一个最小化的正确Spring配置文件应如下所示: ```xml <?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"> <!-- Bean definitions go here --> </beans> ``` #### 4. 使用IDE验证XML文件 使用支持XML Schema验证的IDE(如IntelliJ IDEA、Eclipse)可以帮助检测和修复XML格式错误。IDE通常会在编辑时高亮显示语法错误,并提供自动修复建议。 #### 5. 检查Spring版本Schema版本一致性 确保使用的Spring框架版本XML中引用的Schema版本一致。例如,如果使用的是Spring 5.x,则应使用`spring-beans.xsd`而非`spring-beans-4.0.xsd`。版本不一致可能导致解析失败。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值