`
llyzq
  • 浏览: 576451 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Cannot find the declaration of element 'beans'

    博客分类:
  • J2EE
阅读更多
今天碰到一个奇怪的问题,spring在初始化xml配置文件的时候报错:
Cannot find the declaration of element 'beans'

spring配置文件原来是这样写的:
<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 org/springframework/beans/factory/xml/spring-beans-2.0.xsd"
			 default-lazy-init="true">


由于应用会有无互联网的环境下运行,同事将命名空间的引用改成了
org/springframework/beans/factory/xml/spring-beans-2.0.xsd
但不知道为什么在我本地上运行却不行

最后google了好几次才找到原因

<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-2.0.xsd"
			 default-lazy-init="true">

需要增加“classpath:/”


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

引用

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

今天向服务器部署一个console java app之后,启动时发现程序出现异常:
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’.
然后去看看app-config.xml里的schema声明部分,是这样写的:
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd” >
很明显,有关’beans’的声明都放在http://www.springframework.org/schema/beans/spring-beans-3.0.xsd里面了。可能是网络原因,访问不倒导致解析app-config.xml失败,致使程序抛出异常。
打开spring-beans-3.0.1.RELEASE.jar一看,那个xsd文件就在org.springframework.beans.factory.xml包下面,这下子好办了,把xsd文件的引用全改为本地的就行了:
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
classpath:/org/springframework/context/config/spring-context-3.0.xsd” >
分享到:
评论
4 楼 lihaiming 2017-08-11  
受用了,已把定义的版本改为跟本地的一致,不用担心找不到了,现应用启动正常。
3 楼 Albert_Hawking 2017-01-04  
看到这个帖子,解决了我的问题。 我的总结是这样的:
由于maven的pom文件中配置的<springframework.version>3.1.4.RELEASE</springframework.version>而我的配置文件中使用了高于3.1.4版本的xsd文件,在有网络的情况下不会有问题,没有网络就会有问题,我通过把maven中pom 修改为<springframework.version>3.2.4.RELEASE</springframework.version>
解决了问题。http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 这种XSD配置方式没有问题,访问不了网络时,会到jar包下查找本地化的classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd 。
2 楼 liuxiyangyang 2015-01-28  
文章帮了我大忙了,谢谢
1 楼 java杨正伟 2012-07-15  

正在查一个beans的问题,受用了谢谢!!!

相关推荐

Global site tag (gtag.js) - Google Analytics