java - 将spring项目导入到另一个spring项目中

标签 java eclipse spring spring-mvc

我在 eclipse 中开发了一个 Spring JPA 项目,它可以访问存储在 mysql 服务器中的数据。现在我需要将这个项目导入到 Spring @ MVC 项目中。所以

  1. 我在JPA项目的构建路径中配置导出maven依赖项
  2. 我将项目添加到 tomcat bootstrap 类路径中,
  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>

预先感谢您的帮助

达尼洛

最佳答案

您已经定义了事务管理器:

<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。确保它位于您的网络应用程序的类路径中。

关于java - 将spring项目导入到另一个spring项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9153585/

相关文章:

java - 使用 Spring Security 进行身份验证后,SecurityContextHolder.getPrincipal() 返回 AnonimousUser

eclipse - Maven 依赖项仅在命令行下载,不在 Eclipse 中下载

eclipse - 如何为添加到 Eclipse 工作区的项目使用相对路径?

java - 循环媒体播放器在线程中停止

Java Spring Boot休息服务之争

java - 如何在排序结束时始终有空值

java - 如何使 WireMock 验证提供更多信息?

java - 三元运算符在条件不满足时评估条件语句

java - 相机X : How to call the analyze method with an image

java - Spring WebClient 从 Json 请求中过滤 Null