2016-04-26 15 views
0

データベース(Spring + Hibernate)で動作する「モデル」アプリケーションを作成しましたが、main()メソッドでテストするために起動したときにうまく動作します:Spring MVCでjdbcドライバを作成中に例外がスローされました

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

しかし、私は.jarファイルにそのアプリを構築し、依存関係として私の春のMVC Webアプリケーションに含めると、コントローラ内部で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へのアプリ私は豆をテストするコメント:

<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> 

なぜモデルアプリが正常に動作したのだろうと思います。

+0

問題はハードコードされたアプリケーションコンテキストへのパスにある可能性があります。例えば。春のブートでは、application.propertiesファイルをresourcesフォルダに置き、@PropertySource( "classpath:application.properties")で設定を注釈しました。おそらく、あなたのApplicationContext.xmlをresourcesフォルダ(サブフォルダなし)に入れて、ApplicationContextを試してみてください。applicationContext = 新しいFileSystemXmlApplicationContext( "ApplicationContext.xml") – lenach87

答えて

0

"database.driverClassName"のどこかでこのプロパティを定義する必要があります。それが "com.mysql.jdbc.Driver"に設定されていることを確認してください。

または単にorg.apache.commons.dbcp.BasicDataSourceのBeanを編集し、プロパティ「driverClassName」を「com.mysql.jdbc.Driver」に変更します。エレガントではありませんが、それはあなたを始めなければなりません。

関連する問題