Spring配置文件xsi:schemaLocation无法解析导致启动失败的解决方案

时间:2022-12-14 17:52:23

今天部署一个jar包的时候遇到一个问题:

Exception in thread “main” org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from class path resource [app-config.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ‘beans’

或者:

schema_reference.4:无法读取方案文档 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd,原因是1)无法找到文档 2)无法读取文档 3)文档的根元素不是<xsd:schema>.
caused by java.net.ConnectionException:Connection timed out:connect.

这个很让我无语了,本地都是好好的,为什么在服务器上就不行了,后来才知道服务器是无外网环境的,由于程序用了spring,在加载applicationContext.xml的时候会报上述错误。

主要的原因是在applicationContext.xml文件里要根据namespace找schemaLocation的地址,如果没有配置schemaLocation的话,就找namespace下的地址,由于未联网,所以会失败,解决办法是讲原有的:

<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-4.1.xsd"
       default-lazy-init="false">

改成:

<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
       classpath:/org/springframework/beans/factory/xml/spring-beans-4.1.xsd ">

这样就可以了

参考:

http://blog.csdn.net/dingqinghu/article/details/46758671

http://www.jnan.org/archives/2010/12/cannot-find-the-declaration-of-element-beans.html