1. 程式人生 > >IDEA下Maven專案整合Spring和MyBatis出現jdbc.properties is invalid;前言中不允許有內容

IDEA下Maven專案整合Spring和MyBatis出現jdbc.properties is invalid;前言中不允許有內容

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

解決方法

這裡寫圖片描述

這裡寫圖片描述

正確配置截圖

這裡寫圖片描述