【MedusaSTears】【Spring心得】xmlns=“http://www.spring 和 xmlns:beans=“http://www.spring 差之毫厘谬以千里

运行java测试代码时出现报错,控制台卡住了:

五月 03, 2018 9:11:10 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ef4e59: 
startup date [Thu May 03 21:11:10 CST 2018]; root of context hierarchy
五月 03, 2018 9:11:10 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]

打开junit控制台查看代码如下:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: 
Line 5 in XML document from class path resource [applicationContext.xml] is invalid; 
nested exception is org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 2; cvc-elt.1: 找不到元素'beans' 的声明。

原因是你的xml配置文件头里多了个【:beans】,具体分析如下:

 spring配置里面,正确的文件头设置:

     applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
>
</beans>

注意第3行,写法是

        applicationContext.xml
  1. xmlns="http://www.springframework.org/schema/beans"
    ="http://www.springframework.org/schema/beans"
    

这个是正确的写法,运行时不会出现异常

 错误代码是下面这个:

        applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
>
</beans>
这里的第3行,多了【:beans】

 出现问题的原因是,你在namespace里面,取消了勾选beans,然后又勾选上了beans 虽然按照理论来说没有错误,但是我测试MyEclipse和Eclipse均100%重现这个问题,而且肯定运行代码报错

————————————————————————————

2018.05.03                                

MedusaSTears                          

Writen By  Windows Live Writer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值