Document root element "beans", must match DOCTYPE

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

### 回答1: 这句话的意思是“必须声明元素beans”。它通常出现在XML文件中,表示在使用beans时必须先声明它们。这是因为XML是一种结构化的语言,需要明确指定每个元素的类型和属性。如果没有声明beans元素,XML解析器将无法识别它们,导致解析错误。因此,必须在XML文件中声明所有使用的元素,包括beans。 ### 回答2: “element beans must be declared”这个错误通常发生在使用Spring框架的时候,它表明在XML配置文件中,没有正确地声明一个bean。 接下来,我将详细解释什么是bean,为什么我们需要声明它,以及如何解决这个错误。 在Spring框架中,Bean是指由Spring IoC容器管理的对象。IoC(Inversion of Control)是一种编程模式,它反转了对象之间的控制关系。在IoC模式下,对象的创建、管理和销毁都由容器来处理,而不是由程序员手动进行。 因此,我们需要在XML配置文件中为每个bean提供定义。这个定义包括bean的类型、属性以及依赖关系。Spring框架使用这些定义来创建和管理应用程序中的bean。 当你尝试使用尚未在XML配置文件中声明的bean时,就会出现“element beans must be declared”这个错误。这是因为Spring框架无法识别这个bean,也就无法在应用程序中正确地使用它。 为了解决这个问题,你需要在XML配置文件中为这个bean提供定义。这通常涉及到创建一个bean的元素,该元素指定bean的类、属性和依赖项。例如,一个典型的bean定义可能如下所示: ```xml <bean id="myBean" class="com.example.MyBean"> <property name="propertyName1" value="propertyValue1" /> <property name="propertyName2" value="propertyValue2" /> </bean> ``` 在这个定义中,我们为一个名为myBean的类创建了一个bean。这个bean的类型是com.example.MyBean,并且它有两个属性propertyName1和propertyName2。 总之,当出现“element beans must be declared”这个错误时,说明你需要在XML配置文件中声明一个bean。你需要提供一个bean的定义,包括bean的类型、属性和依赖项。这个定义让Spring框架可以正确地识别和管理bean,以便使应用程序正常运行。 ### 回答3: element beans must be declared 是 Spring Framework 中的一个常见错误提示。该错误通常出现在 Spring 配置文件中,表明 XML 配置文件中未声明 beans 元素。 在 Spring 中,XML 配置文件可以用来声明应用程序中的组件,其中最重要的是 beans 元素。beans 元素是 Spring IoC 的核心组件,它是一个包含所有 bean 声明的容器。在 Spring 应用程序中,所有的 bean 都应该在 beans 元素中声明。如果在配置文件中未声明 beans 元素,则会出现 element beans must be declared 的错误。 要解决这个问题,可以简单地在 XML 配置文件中添加一个 beans 元素。例如: ``` <?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <!-- 在这里添加 bean 的声明 --> </beans> ``` 在这个例子中,xmlns 指定了 beans 的命名空间,而 xsi:schemaLocation 指定了 beans 元素的 XSD 文件位置。在这个 beans 元素中,可以添加任意数量的 bean 声明,每个 bean 声明都应该有一个唯一的 id 和 class 属性。 总之,element beans must be declared 错误通常是由于未声明 beans 元素所致。通过添加 beans 元素并在其中声明需要的 bean,可以解决此问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值