项目在本地运行正常,打包放在服务器上之后出现了以下报错
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 13 in XML document from class path resource [spring-servlet.xml] is
invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 82; cvc-complex-type.2.4.c: The matching wildcard is strict, bu
t no declaration can be found for element 'context:component-scan'.
......
because 1) could not find the document; 2) the document could not be read; 3) the root
element of the document is not .
打包的过程中spring-servlet.xml文件中有时候出现报错,有时候不会出现,不影响打包的过程。
从错误提示来看,是解析spring-servlet.xml文件的时候找不到解析标签用的xsd文件,或是xsd文件不正常导致解析出错
spring-servlet.xml文件部分内容如下:
在网上找到了一些比较靠谱的解释和方案:
1.Spring默认在启动时是要加载XSD文件来验证xml文件的,所以如果有的时候断网了,或者一些开源软件切换域名,那么就很容易碰到应用启动不了。
为了防止这种情况,Spring提供了一种机制,默认从本地加载XSD文件。spring的jar包中就包含了这个文件。
参考文章:
为什么在Spring的配置里,最好不要配置xsd文件的版本号
这篇文章中解释了相关的概念
2.在Eclipse中配置,加载本地文件
参考文章:
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 问题的解决
这种方式可以解决本地文件的报错,但是打包后依旧不好使
在Eclipse中配置的过程中,发现有些高版本的xsd文件中引用了低版本的xsd文件。jar包中包含了当前版本往前的所有xsd文件。
【问题解决】
经过一番搜索翻阅,发现这两个文件很重要:spring.schemas,spring.handlers,前者是为了验证你自定义的xml配置文件是否符合你的格式要求,后者是告诉spring该如何来解析你自定义的配置文件。
仔细看了看打完包之后的文件。发现包中META-INF目录下的俩文件
spring.handlers和spring.schemas中的内容与项目中使用到的不一致,有缺失
包中的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/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
http\://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd
http\://www.springframework.org/schema/beans/spring-beans-4.0.xsd=org/springframework/beans/factory/xml/spring-beans-4.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-4.1.xsd=org/springframework/beans/factory/xml/spring-beans-4.1.xsd
http\://www.springframework.org/schema/beans/spring-beans-4.2.xsd=org/springframework/beans/factory/xml/spring-beans-4.2.xsd
http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-4.2.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/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd
http\://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd
http\://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd
http\://www.springframework.org/schema/tool/spring-tool-3.2.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd
http\://www.springframework.org/schema/tool/spring-tool-4.0.xsd=org/springframework/beans/factory/xml/spring-tool-4.0.xsd
http\://www.springframework.org/schema/tool/spring-tool-4.1.xsd=org/springframework/beans/factory/xml/spring-tool-4.1.xsd
http\://www.springframework.org/schema/tool/spring-tool-4.2.xsd=org/springframework/beans/factory/xml/spring-tool-4.2.xsd
http\://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-4.2.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/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd
http\://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd
http\://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd
http\://www.springframework.org/schema/util/spring-util-3.2.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd
http\://www.springframework.org/schema/util/spring-util-4.0.xsd=org/springframework/beans/factory/xml/spring-util-4.0.xsd
http\://www.springframework.org/schema/util/spring-util-4.1.xsd=org/springframework/beans/factory/xml/spring-util-4.1.xsd
http\://www.springframework.org/schema/util/spring-util-4.2.xsd=org/springframework/beans/factory/xml/spring-util-4.2.xsd
http\://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-4.2.xsd
与spring-servlet.xml文件头部相比,缺少了以下几个xsd文件的映射
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/task/spring-task-4.2.xsd
只要按照spring.schemas文件里现有的映射关系,再加上那些没有的映射就可以啦~
注意,要将低版本的映射关系也加上,有些高版本的文件引了低版本的文件
包中的spring.handlers
http\://www.springframework.org/schema/c=org.springframework.beans.factory.xml.SimpleConstructorNamespaceHandler
http\://www.springframework.org/schema/p=org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler
http\://www.springframework.org/schema/util=org.springframework.beans.factory.xml.UtilNamespaceHandler
同样的,再配置上相关的handler
http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler
http\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler
替换包中的这两个文件后,上传运行,问题解决~
再记录一篇很不错的文章:
Spring自定义标签和spring.handlers的加载过程