IDEA下Maven项目整合Spring和MyBatis出现jdbc.properties is invalid;前言中不允许有内容

9 篇文章 0 订阅
2 篇文章 0 订阅

Idea下用Maven管理SpringMyBatis整合的项目,在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

解决方法

这里写图片描述

这里写图片描述

正确配置截图

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值