在Idea下用Maven
管理Spring
和MyBatis
整合的项目,在Junit
测试service层代码时不会出错,但把整个项目发布到Tomcat
时抛出各种各样的异常,花了最多时间的异常为:
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
Line 1 in XML document from class path resource [jdbc.properties] is invalid;
nested exception is org.xml.sax.SAXParseException;
lineNumber: 1; columnNumber: 1; 前言中不允许有内容。
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。
解决此异常的思路
检查项目中的各类配置文件
首先检查spring框架的配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
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">
<!--c3p0数据源(properties文件配置)-->
<!--<context:property-placeholder location="classpath:jdbc.properties"/>-->
<context:property-placeholder location="jdbc.properties"/>
<bean id="c3p0Datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--c3p0数据源(直接配置)-->
<!--<bean id="c3p0Datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
<!--<property name="driverClass" value="com.mysql.jdbc.Driver"/>-->
<!--<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/conference"/>-->
<!--<property name="user" value="root"/>-->
<!--<property name="password" value="qwer123456"/>-->
<!--</bean>-->
<!-- 创建-SqlSessionFactory对象 -->
<bean id="mysqlSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="dataSource" ref="c3p0Datasource"/>
</bean>
<!-- 生成Dao代理对象-->
<bean id="userinfoDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="mysqlSqlSessionFactory"/>
<property name="mapperInterface" value="modeldao.UserinfoDao"/>
</bean>
<!-- 扫描式动态代理-->
<!-- 配置为指定包中的所有接口生成代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="mysqlSqlSessionFactory"/>
<property name="basePackage" value="modeldao"/>
</bean>
<bean id="scurityGuaranteeService" class="service.ScurityGuaranteeService">
<property name="dao" ref="securityGuaranteeDao"/>
</bean>
</beans>
初始有异常的web.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml classpath:jdbc.properties</param-value>
</context-param>
<!-- 注册ServletContext监听器-->
<!-- 1. 在ServletContext被创建时,创建Spring容器对象-->
<!-- 2. 把创建的Spring容器对象放入Application域中-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
jdbc.properties内容如下:
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/conference
jdbc.user = root
jdbc.password = qwer123456
解决方法