一次马虎导致的异常: 文档根元素 "beans" 必须匹配 DOCTYPE 根 "null"

时间:2025-04-11 08:04:20

一次马虎导致的异常: 文档根元素 "beans" 必须匹配 DOCTYPE 根 "null"


使用xml 配置spring. 启动程序时突然抛出: 文档根元素 “beans” 必须匹配 DOCTYPE 根 “null”. 网上搜索原因有:

  • spring 版本依赖问题.
  • beans 元素没有终止元素. 看xml 配置.

从下面的xml 文件看不是上面列出的原因

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
       xmlns:xsi="http:///2001/XMLSchema-instance"
       xmlns:p="/schema/p"
       xmlns:context="/schema/context"
       xmlns:tx="/schema/tx"
       xsi:schemaLocation="/schema/beans
       /schema/beans/
       /schema/context
       /schema/context/
       /schema/tx
       /schema/tx/
">
    <context:property-placeholder location="classpath:"/>
    <context:component-scan base-package="."/>
    <bean class="" 
          p:driverClassName="${}"
          p:url="${}"
          p:username="${}"
          p:password="${}"
          destroy-method="close"
    />

    <bean class=""
          
          p:dataSource-ref="dataSource"/>

    <bean class="" 
    p:dataSource-ref="dataSource"/>

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

一个一个排除试试

  • 如果把beans 中所有元素都注释掉. spring 抛出 异常.
  • 一个一个元素注释掉.
    • <context:property-placeholde/> 注释掉后抛出 Could not open JDBC Connection for transaction; nested exception is : Cannot load JDBC driver class '${}'
    • <context:component-scan/> 元素注释掉后抛出 文档根元素 “beans” 必须匹配 DOCTYPE 根 “null”。

应该是最后面的原因. 原来<context:property-placehole locatio=""/> 的值配置错了. location 修改为指定的properties 文件后正常运行.