一次马虎导致的异常: 文档根元素 "beans" 必须匹配 DOCTYPE 根 "null"
使用xml 配置spring. 启动程序时突然抛出: 文档根元素 “beans” 必须匹配 DOCTYPE 根 “null”. 网上搜索原因有:
- spring 版本依赖问题.
- beans 元素没有终止元素. 看xml 配置.
从下面的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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<context:property-placeholder location="classpath:application.xml"/>
<context:component-scan base-package="cn.sheing.pead.chap12.connleak"/>
<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"
p:driverClassName="${database.driverName}"
p:url="${database.url}"
p:username="${database.username}"
p:password="${database.password}"
destroy-method="close"
/>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
id="transactionManager"
p:dataSource-ref="dataSource"/>
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate"
p:dataSource-ref="dataSource"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
一个一个排除试试
- 如果把beans 中所有元素都注释掉. spring 抛出org.springframework.beans.factory.NoSuchBeanDefinitionException 异常.
- 一个一个元素注释掉.
- <context:property-placeholde/> 注释掉后抛出 Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${database.driverName}'
- <context:component-scan/> 元素注释掉后抛出 文档根元素 “beans” 必须匹配 DOCTYPE 根 “null”。
应该是最后面的原因. 原来<context:property-placehole locatio=""/> 的值配置错了. location 修改为指定的properties 文件后正常运行.