Document root element "beans", must match DOCTYPE root "null"分析及解决方法

 解决办法:

1. 在你的工程目录下查找是否含有两个或者多个 spring.jar 包,如:是否有 spring.jar 并且含有 其他

 

    spring-1.3.jar 包。如果同时存在多个,则只留其中一个,因为解析 xml 的时候版本会不兼容。

 2. 如果这样还是没有解决的话,检查你的 applicationContext.xml  的 DOCTYPE。

   <?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-2.0.xsd">

    ......

    </beans>

  如果是这种解析方式,则将 xml 的 DOCTYPE 换成如下:

    <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>

       ......

    </beans>

  解析方式不同,也是造成这个异常的原因之一。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Ghost_520/archive/2009/03/25/4022584.aspx

 

另外的解决办法:

Document root element "beans", must match DOCTYPE root "null".的错误提示,网上很多人说要把applicationContex.xml文件中加上如下第二行的<!DOCTYPE/>标签,说明DTD,其实并不准确。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
……
</beans>
实例中的配置文件时beans-config.xml:
<?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-2.0.xsd">
     
    <bean id="helloBean"
          class="onlyfun.caterpillar.HelloBean">
        <property name="helloWord">
            <value>Hello!Justin!</value>
        </property>
    </bean>
</beans>
很显然这是XML Schema的设置方式。我把它改为XML DTD的设置方式,如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
 <beans>    
    <bean id="helloBean"
          class="onlyfun.caterpillar.HelloBean">
        <property name="helloWord">
            <value>Hello!Justin!</value>
        </property>
    </bean>
</beans>
再次运行,OK,成功!后来在spring forum上发现了正解:
You have the wrong xml configuration for the version of spring.

1.x use DOCTYPE
2.x use schema

You must have 1.x in the classpath.
引自http://forum.springframework.org/showthread.php?t=37883

    现在明白了,spring 1.x 使用DOCTYPE,而2.x是用schema,我的项目出错原因是由于前面的其他错误怀疑spring版本问题把spring2.0换成了spring1.2,而我使用的Spring IDE是以前配置的Spring1.x版本。改用配置文件为XML DTD解析方式就可以了,或者更新Spring IDE为2.x版本也可以解决问题。
 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/foamflower/archive/2008/04/20/2310041.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答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、付费专栏及课程。

余额充值