在Spring MVC中创建jdbc驱动程序时抛出异常

时间:2016-04-26 17:17:00

标签: java spring spring-mvc jdbc

我已经创建了"模型"适用于数据库(Spring + Hibernate)的应用程序,当我使用main()方法进行测试时它工作正常:

ApplicationContext applicationContext =
            new FileSystemXmlApplicationContext("src/main/resources/META-INF/spring/ApplicationContext.xml");
    CityService service =
            (CityService) applicationContext.getBean("cityService");
    //all other methods

但是当我将该应用程序构建到.jar文件中并将其作为依赖项包含在我的Spring MVC webapp中并尝试在控制器中使用CityService时,它会引发异常:

org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'org.hibernate.dialect.MySQLDialect' for connect URL 'jdbc:mysql://localhost:3306/delivery'
...
java.sql.SQLException: No suitable driver

建立"模型"应用程序到.jar我评论测试bean:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
   xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />

<context:spring-configured />

<context:component-scan base-package="com.userok.pet.delivery.model">
    <context:exclude-filter expression=".*_Roo_.*"
                            type="regex" />
    <context:exclude-filter expression="org.springframework.stereotype.Controller"
                            type="annotation" />
</context:component-scan>

<bean class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close" id="dataSource">
    <property name="driverClassName" value="${database.driverClassName}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
    <property name="testOnBorrow" value="true" />
    <property name="testOnReturn" value="true" />
    <property name="testWhileIdle" value="true" />
    <property name="timeBetweenEvictionRunsMillis" value="1800000" />
    <property name="numTestsPerEvictionRun" value="3" />
    <property name="minEvictableIdleTimeMillis" value="1800000" />
</bean>

<bean class="org.springframework.orm.jpa.JpaTransactionManager"
      id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<!--beans used for testing-->
<!--<bean class="com.userok.pet.delivery.model.service.CargoService"
        id="packageService"/>
<bean class="com.userok.pet.delivery.model.dao.CargoDAO"
        id="packageDao"/>
<bean class="com.userok.pet.delivery.model.service.CityService"
        id="cityService"/>
<bean class="com.userok.pet.delivery.model.dao.CityDAO"
        id="cityDao"/>
-->

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

<bean
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="dataSource" ref="dataSource" />
</bean>

我想知道为什么这会发生,因为模型应用程序正常工作。

1 个答案:

答案 0 :(得分:0)

您应该在某处定义此属性&#34; database.driverClassName&#34;。确保将其设置为&#34; com.mysql.jdbc.Driver&#34;。

或者只需编辑bean org.apache.commons.dbcp.BasicDataSource并更改属性&#34; driverClassName&#34; to&#34; com.mysql.jdbc.Driver&#34;。不优雅,但它应该让你开始。