2012-02-05 184 views
0

我在eclipse中開發了一個Spring JPA項目,它訪問存儲在mysql服務器中的數據。現在我需要在Spring @ MVC項目中導入這個項目。所以將彈簧項目導入另一個彈簧項目

  1. 我在JPA項目的構建路徑配置導出Maven依賴
  2. 我添加項目到Tomcat引導類路徑,
  3. 我加入了JPA/Spring項目到類路徑我的Spring @MVC項目
  4. 我還將jpa應用程序上下文添加到MVC項目的根上下文中。

導入背景:

<import resource="classpath:/META-INF/spring/app-context.xml"/> 

。但是當我在tomcat上啓動項目時出現錯誤。看來我需要將相同的JPA項目庫導入到MVC項目中。以下是錯誤日誌:

GRAVE: Excepción enviando evento inicializado de contexto a instancia de escuchador de 
clase org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' defined in class path resource [META-INF/spring/app-context.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation. 
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor] for bean with name 'org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0' defined in class path resource [META-INF/spring/app-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor 
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] for bean with name 'entityManagerFactory' defined in class path resource [META-INF/spring/app-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean 
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.jpa.JpaTransactionManager] for bean with name 'transactionManager' defined in class path resource [META-INF/spring/app-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.jpa.JpaTransactionManager 

這裏是我的根上下文: http://www.springframework.org/schema/beans/spring-beans-3.0.xsd「>

<import resource="classpath:/META-INF/spring/app-context.xml"/> 

    <!-- 
    <import resource="classpath:/META-INF/spring/app-context.xml"/> 
    --> 
    <!-- Root Context: defines shared resources visible to all other web components --> 

</beans> 

這裏是JPA項目的我的應用程序上下文:

<?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" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <description>Example configuration to get you started.</description> 

    <!-- Generic --> 
    <context:annotation-config /> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <!-- JPA --> 

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 
    <tx:annotation-driven /> 
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="showSql" value="true" /> <!-- Prints used SQL to stdout --> 
       <property name="generateDdl" value="true" /> <!-- Generates tables. --> 
       <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" /> 
      </bean> 
     </property> 
    </bean> 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://localhost:3306/windydb"/> 
     <property name="username" value="windyuser"/> 
     <property name="password" value="maverick1984"/> 
    </bean> 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 
    <context:component-scan base-package="com.windy.spring" /> 

</beans> 

在此先感謝您的幫助

達尼洛

+0

發佈app-context.xml會很有幫助。 – Paul 2012-02-05 22:21:55

+0

也許它可以幫助http://stackoverflow.com/questions/8434712/no-persistence-exception-translators-found-in-bean-factory-cannot-perform-excep – HRgiger 2012-02-05 22:23:44

+0

@Paul我已經把我的應用程序的上下文,以問題 – ddelizia 2012-02-05 22:36:16

回答

2

你已經爲你的事務管理器:

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

但Tomcat的找不到類org.springframework.orm.jpa.JpaTransactionManager。確保它在你的webapp的類路徑中。

相關問題