xml文件一定要和它的spring.jar包匹配,否则就会出错
错误原因分析:
spring1.X使用DOCTYPE,而2.X版本使用schema。我们使用低版本的spring就要配置文件使用dtd解析方式,使用高版本的就要使用xsl解析方式。
解决方法:
加上
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
去掉
<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-2.5.xsd">
---------------------------------------------------
报错:org.xml.sax.SAXParseException:Document root element "beans", must match DOCTYPE
原因:spring配置文件中没有声明:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
如果bean要声明scope或者singleton
在spring2.0之前bean只有2种作用域即:singleton(单例)、non-singleton(也称prototype), Spring2.0以后,增加了session、request、global session三种专用于Web应用程序上下文的Bean。
<!-- using spring-beans-2.0.dtd -->
<bean id="accountService" scope="prototype"/>
<!-- the following is equivalent and preserved for backward compatibility in spring-beans.dtd -->
<bean id="accountService" singleton="false"/>
如果你用"singleton"属性那么就必须在那个文件里 引用'spring-beans.dtd' DTD。 如果你用"scope"属性那么必须 在那个文件里引用'spring-beans-2.0.dtd' DTD 或'spring-beans-2.0.xsd' XSD