运行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
-
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