Spring加载xsd引起的问题小记

前言

  最近要把之前写好的监控系统加上报警功能,就是通过rpc调用发短信发邮件的服务发送报警信息。发短信发邮件的功能是通过dubbo管理提供的。自然使用这些服务就难免用到spring。而我这又是一个storm工程,为了方便,我都是用maven-shade-plugin把所有依赖的jar打到一起。

  在本地运行没有任何问题,但已提交到在线环境的storm(在线环境是没法连接到外网的),就会报错:

spring cvc-elt.1: Cannot find the declaration of element ‘beans‘

  对于这个错误,网上有很多解决方法,其本质就是找不到对应的xsd文件。但好多解决方法都是碰巧并不是对问题本质有了了解后的解决方式。所以我在这里记录下自己的解决方式。

spring加载xsd文件的流程

  要解决上述问题,就需要理解spring加载xsd文件的流程:

  1、spring首先会读取spring jar包下META-INF下的spring.schemas,根据spring.schemas里的映射路径在本地找xsd文件。

  2、如果在本地找不到xsd文件,spring会通过配置文件里的url到官网上下载xsd文件。

发生问题的原因

  通过上述加载xsd的流程,已改可以预见发生问题的原因:

  1、在不能访问外网的情况下,使用的xsd的版本高于使用的spring的版本,spring jar包里没有对应版本的xsd导致

  2、在不能访问外网的情况下,jar包的META-INF目录下的spring.schemas被破坏,没法找到xsd的映射路径

  知道了上述这些之后,我打开了我用maven-shade-plugin打好的jar包,找到META-INF下的spring.schemas,发下里边的映射路径果然不全。掐指一算就知道是maven-shade-plugin打包的问题,它没有将各个jar包里的spring.schemas内容合并到一起,而是采用了覆盖的方式。知道了这个原因后,在maven-shade-plugin里配置如下:

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

然后就这样完美解决了。

  据说使用maven-assembly-plugin打包也会出现此问题,所以要当心了

  

时间: 2024-07-30 19:58:44

Spring加载xsd引起的问题小记的相关文章

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

本文原文连接: http://blog.csdn.net/bluishglc/article/details/7596118 ,转载请注明出处! 有时候你会发现过去一直启动正常的系统,某天启动时会报出形如下面的错误: [plain] view plaincopy org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/sche

(Spring加载xml时)org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element &#39;beans&#39;.

ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");报错 在启动Spring时,报以下错误,如图: 原因是在xml中spring的xsd的版本配置的不一致,我使用的是spring-2.5.6,但配置文件中配的是3.0.改成如下即可: [xhtml] view plain copy <?xml version="1.0" encoding="UTF-8"

spring加载jar包中的多个配置文件[转载]

在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:beanconfigs/applicationContext_1.xml, classpath*:beanconfigs/applicationContext_2.xml, ...

spring加载jar包中多个配置文件(转)

转自:http://evan0625.iteye.com/blog/1598366 在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:beanconfigs/applicationContext_1.xml, classpath*:

spring加载配置文件

spring加载配置文件 1.把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式2.采用在web.xml中配置ContextLoaderListenera或ContextLoaderServlet指定加载路径方式.3 通过ClassPathXmlApplicationContext或XmlWebApplicationContext代码动态加载!

【转载】Spring加载resource时classpath*:与classpath:的区别

免责声明:     本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除.     原文作者:kyfxbl     原文地址: spring配置中classpath和classpath*的区别   在spring配置文件里,可以用classpath:前缀,来从classpath中加载资源  比如在src下有一个jdbc.properties的文件,可以用如下方法加载: <bean id="propertyConfigurer" class="

J2EE加载资源文件以及Spring加载XML文件

J2EE加载XML文件 Resource接口,是用来加载文件的接口. FileSystemResource和ClassPathResource都是实现了Resource接口,他们都可以用来加载XML文件. 具体代码如下: 1 Resource resource1 = new ClassPathResource("文件.xml"); 2 3 Resource resource2 = new FileSystemResource("盘符:/项目路径/src/文件.xml"

is not mapped问题,Spring加载jar中配置文件

错误如下: org.hibernate.hql.ast.QuerySyntaxException: Content is not mapped [select new Content (t.id,t.name,t.values,t.systemType,t.type,t.sortnumber) from Content t where 1=1 and t.type = ? and t.systemType = ? order by t.sortnumber desc ] at org.hiber

spring加载过程中jar包加载不了,解决方法

当我们在开发spring项目时,一般会将jar包放到webInf/lib下,这样是myeclipse自动将jar包加载到tomcat中webapps下,但是当我们新建一个lib文件夹的情况下,我们add building Path时就会出错,这时候我们有个技巧供使用. 1.项目上点击右键搜索de,找到deployment assembly 目的就是将此处添加的jar包添加到系统webINF/lib路径下 来自为知笔记(Wiz) spring加载过程中jar包加载不了,解决方法