有时候你会发现过去一直启动正常的系统,某天启动时会报出形如下面的错误:

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans-2.0.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>.

很显然,spring xml配置文件中指定的xsd文件读取不到了,原因多是因为断网或spring的官网暂时无法连接导致的。 你可以通过在浏览器输入xsd文件的URL,如:http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 进行确认。

关于这个问题,网上有两种常见的解决方法,第一种简单有效,但是工作量大,即:把所有spring配置文件中url形式的xsd路径转换成指向本地xsd文件的classpath形式的路径,例如:classpath:org/springframework/beans/factory/xml/spring-beans-2.5.xsd ,再有一种方法就是在本机搭建web服务器,按URL创建相应文件夹,放入对应xsd文件,在本机hosts文件中加入"127.0.0.1 www.springframework.org".实际上,这两种方法都属于“歪打正着”式的方法,直正弄明白这一问题还需要从spring的XSD文件加载机制谈起。

首先:你必须知道一点:spring在加载xsd文件时总是先试图在本地查找xsd文件(spring的jar包中已经包含了所有版本的xsd文件),如果没有找到,才会转向去URL指定的路径下载。这是非常合理的做法,并不像看上去的那样,每次都是从站点下载的。事实上,假如你的所有配置是正确定的,你的工程完全可以在断网的情况下启动而不会报上面的错误。Spring加载xsd文件的类是PluggableSchemaResolver,你可以查看一下它的源码来验证上述说法。另外,你可以在log4j.xml文件中加入:

 <logger name="org.springframework.beans.factory.xml"><level value="all" /></logger>

通过日志了解spring是何加载xsd文件的。

接下来,问题就是为什么spring在本地没有找到需要的文件,不得不转向网站下载。关于这个问题,其实也非常简单。在很多spring的jar包里,在META-INF目录下都有一个spring.schemas,这是一个property文件,其内容类似于下面:

http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
....

实际上,这个文件就是spring关于xsd文件在本地存放路径的映射,spring就是通过这个文件在本地(也就是spring的jar里)查找xsd文件的。那么,查找不到的原因排除URL输入有误之外,可能就是声明的xsd文件版本在本地不存在。一般来说,新版本的spring jar包会将过去所有版本(应该是自2.0以后)的xsd打包,并在spring.schemas文件中加入了对应项,出现问题的情况往往是声明使用了一个高版本的xsd文件,如3.0,但依赖的spring的jar包却是2.5之前的版本,由于2.5版本自然不可能包含3.0的xsd文件,此时就会导致spring去站点下载目标xsd文件,如遇断网或是目标站点不可用,上述问题就发生了。

但是,在实现开发中,出现上述错误的几率并不高,最常见的导致这一问题的原因其实与使用了一个名为“assembly”的maven打包插件有关。很多项目需要将工程连同其所依赖的所有jar包打包成一个jar包,maven的assembly插件就是用来完成这个任务的。但是由于工程往往依赖很多的jar包,而被依赖的jar又会依赖其他的jar包,这样,当工程中依赖到不同的版本的spring时,在使用assembly进行打包时,只能将某一个版本jar包下的spring.schemas文件放入最终打出的jar包里,这就有可能遗漏了一些版本的xsd的本地映射,进而出现了文章开始提到的错误。如果你的项目是打成单一jar的,你可以通过检查最终生成的jar里的spring.schemas文件来确认是不是这种情况。而关于这种情况,解决的方法一般是推荐使用另外一种打包插件shade,它确实是一款比assembly更加优秀的工具,在对spring.schemas文件处理上,shade能够将所有jar里的spring.schemas文件进行合并,在最终生成的单一jar包里,spring.schemas包含了所有出现过的版本的集合!

以上就是spring加载XSD文件的机制和出现问题的原因分析。实际上,我们应该让我们工程在启动时总是加载本地的xsd文件,而不是每次去站点下载,做到这一点就需要你结合上述提及的种种情况对你的工程进行一番检查。

上述内容转自:http://blog.csdn.net/bluishglc/article/details/7596118

bluishglc的博客分析的原理很到位,认真看下,可以加深对spring加载xsd文件的理解和扩开思路。

以下是我自己的解决方案:

出错提示

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


问题剖析

1、验证网址,把以下网址拷贝到浏览器

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

能正确获取到对应的文件内容,故此网址不存在书写上的错误

2、打开项目中引用的对应spring包的META-INF里的spring.schemas文件

http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http\://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd
http\://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
http\://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd
http\://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd
http\://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd

http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http\://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd
http\://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd
http\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
http\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd
http\://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd
http\://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd

映射信息也没有问题

3、尝试修改xsd的版本号为3.0

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

改为:

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

或者去掉版本号让myeclipse选择默认的

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

运行程序的时候就没有出错提示了。


解决措施:

问题来了,我的spring使用的是2.0,为什么把版本修改为3.0就可以了呢?


从bluishglc的博客分析到,工程启动时候加载xsd是本地优先原则


如此可以推测,本地应该还有个对应的缓存目录保持这些信息。

经过数小时的折腾,总算有眉目了。

打开 myeclipse的对应目录, windows -> General -> Network Connectior -> Cache

里面果然储存了一个版本为3.0的xsd文件

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

把该文件删掉,重启下myeclipse(不重启myeclipse可能不会生效,至少我的是这样),再次运行的时候就不会出错了。

出错原因可能是我曾经使用过较高级的版本,服务器自动下载了3.0.xsd保持在缓存目录,工程每次启动的时候会优先读取到它,把缓存里的其它版本都删除掉,或者把全部缓存删除掉就没问题了。

以上是个人的一些心得,更多的是基于猜测,可能很多理解不到位,敬请指教。

org.xml.sax.SAXParseException: Failed to read schema document错误的完美解决方法 以及 Spring如何加载XSD文件相关推荐

  1. Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)...

    本文原文连接: http://blog.csdn.net/bluishglc/article/details/7596118 ,转载请注明出处! 有时候你会发现过去一直启动正常的系统,某天启动时会报出 ...

  2. org.xml.sax.SAXParseException: The markup in the document following the root element must be well-fo

    部署项目报错:org.xml.sax.SAXParseException: The markup in the document following the root element must be ...

  3. Spring容器启动时出现Failed to read schema document错误

    2019独角兽企业重金招聘Python工程师标准>>> schema_reference.4: Failed to read schema document 'tx-3.2.xsd' ...

  4. 【踩坑】org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 33; schema_reference.4: 无法读取方案文档

    问题: bash org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 33; schema_reference.4: 无法读取方案 ...

  5. Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法

      标签: dubboxmleclipse 2014-10-31 10:25  15976人阅读  评论(0)  收藏  举报 目录(?)[+] 我们公司使了阿里的dubbo,但是阿里的开源网站htt ...

  6. Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.

    信息: Initializing Spring root WebApplicationContext log4j:WARN No appenders could be found for logger ...

  7. 解决:IDEA导入Spring项目,org.xml.sax.SAXParseException: schema_reference.4: 无法读取方案文档 报错

    具体怎么导入看这个大佬的文章: https://www.cnblogs.com/git-niu/p/7685886.html 这里只记录一点小小的问题 报错信息: org.xml.sax.SAXPar ...

  8. org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 110; schema_reference.4: 无法读取方案文档

    异常信息: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 110; schema_reference.4: 无法读取方案文档 ...

  9. org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 105; cvc-elt.1: 找不到元素 'beans' 的声明。

    <span style="color:#FF0000;">Caused by: org.xml.sax.SAXParseException; lineNumber: 9 ...

最新文章

  1. 《TCP/IP详解卷1:协议》第6章 ICMP:Internet控制报文协议-读书笔记
  2. linux命令tree
  3. 分享30个激励的非营利网站设计精美案例
  4. 天津大学仁爱学院c语言期末考试题,天津大学《C语言程序设计》2016年7月考试期末大作业...
  5. HDU 1244 DP
  6. as3 访问远程计算机,本地swf不能访问网络的解决办法
  7. MySQL8.0.x 版本安装步骤傻瓜式教程【官方版】
  8. 程序员选手机那些事儿
  9. MyBatis架构图
  10. Windows 7 SP1 旗舰版 MSDN原版
  11. 如何将qrc文件添加至VS
  12. Fiji-imageJ 无法打开
  13. Python海龟turtle画椭圆方法
  14. XMind2020介绍、下载
  15. RISC-V IDE MRS使用笔记(七) :常用开发技巧汇总
  16. android view硬件加速,Android TextureView和硬件加速
  17. 直播开发中音画不同步如何解决
  18. 石油大学计算机网络课程设计(在线考试答案)
  19. C# 文件以txt文档模式存储,存储地址的设置 saveFileDialog控件的使用
  20. JavaScript运算符详解说明

热门文章

  1. PhpStorm调用浏览器运行php文件
  2. 前端文件path路由:基于base引用
  3. wordpress插件-Media folder插件汉化版_优化加速插件
  4. 搜狗输入法电脑版_搜狗输入法Mac版更新:找不到哪里下载?看这里
  5. 循环斐波那契数列_每日一课 | 斐波那契数列的第n个项
  6. vue请求封装,Store-状态管理部分(文档笔记)
  7. android过滤数字,android – GPS卫星数量和位置过滤
  8. 麦芒装饰装修小程序源码V3.2.59
  9. PHP涟漪云-蓝奏云挂载直连下载程序源码
  10. 一款非常简约好看的白色网格个人引导页HTML源码