带有 JPA 的 Spring MVC:无法从 jar 实例化 JpaRepository [英] Spring MVC with JPA: Not able to instantiate JpaRepository from a jar

查看:50
本文介绍了带有 JPA 的 Spring MVC:无法从 jar 实例化 JpaRepository的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将标准 spring 项目 P1 导入到 Web 项目 P2 中,并且我正在 P2 中实现 P1 的 Web 方面.我已经将P2中P1的所有资源导入为P1.jar还使用 显式导入了应用程序上下文文件,该文件成功导入.但是 JpaRepositories 不会在 P2 中自动装配.似乎不是在 P2 的上下文中.

I am trying to import a standard spring project P1 into a web project P2 and I am implementing the web aspect of P1 in P2. I have imported all resources of P1 in P2 as P1.jar Have explicitly imported the application context file as well using <import-resource> which happens successfully. But the JpaRepositories does not get autowired in P2. It doesn't seem to be in the context of P2.

谁能建议我在这里可能遗漏了什么.

Can anyone suggest what I might be missing here.

更新:2016 年 11 月 25 日

P1-ApplicationContext.xml

P1-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"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:repository="http://www.springframework.org/schema/data/repository"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.11.xsd">

<tx:annotation-driven proxy-target-class="true"/>
 <context:component-scan base-package="com.home.p1.blog" />
    <jpa:repositories base-package="com.home.p1.blog.repo" />


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
        <property name="url"
            value="jdbc:derby://localhost:1527/MyDerby" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="WorkUp" />
        <property name="dataSource" ref="dataSource"></property>
        <property name="packagesToScan" value="com.home.p1.blog"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect" />
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.use_sql_comments">false</prop>
            </props>
        </property>

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

  <bean id="authorDAO" class="com.home.p1.blog.repo.AuthorDAO">

  </bean>
  <bean id="authorService" class="com.home.p1.blog.service.AuthorServiceImpl"></bean>

  <bean id="blogService" class="com.home.p1.blog.service.BlogServiceImpl"></bean>

</beans>

P2-ApplicationContext.xml(它的实际名称是:rest-servlet.xml,用来存放RestController扫描包)

P2-ApplicationContext.xml (its actually named: rest-servlet.xml to hold the RestController scan package)

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:repository="http://www.springframework.org/schema/data/repository"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.11.xsd">
    <import resource="classpath:com.home.p1.blog.src/src/main/resources/P1-ApplicationContext.xml"/> <!-- this loads find -->
       <context:component-scan base-package="com.home.p2.blog.controller" /> <!-- this will load the RestController -->
    <!-- <context:component-scan base-package="com.home.**" />   -->
    <!-- THIS GIVES SOME WIERD ERROR <jpa:repositories base-package="com.home.*" /> -->
    <mvc:annotation-driven/>


<!-- <bean id="blogService" class="com.oracle.blog.service.BlogServiceImpl"></bean> -->
</beans>

更新:2016 年 11 月 28 日在 P2 的应用程序上下文中包含 配置后,我不再遇到之前遇到的奇怪错误.此外,在 Spring bean 支持中成功导入 xml 配置,其他一切都适当地就位.只有 P1 中引用的 JpaRepositories 没有反映在 P2 的上下文中.

UPDATE: 28Nov2016 Upon including <jpa:repositories base-package> configuration in P2's application context, I no longer get the wierd error which I was getting earlier. Also, enabling a successful import of xml config in Spring bean support, everything else is falling appropriately into place. Only the JpaRepositories referred in P1 are not reflecting in P2's context.

推荐答案

RESOLVED: 请注意,要在 P2 中从 P1.jar 导入 JpaRepositories,我们不必提及 标记在 P2 的配置 xml 中.只需确保我们正确导入 P1 的 config.xml.Eclipse 用户还需要在 Spring Beans 支持选项卡中添加 Xml/Java 配置,并启用对导入元素的支持.

RESOLVED: Please note that to import the JpaRepositories from P1.jar in P2, we don't have to mention <jpa:repositories> tag in P2's config xml. Just ensure that we are appropriately importing the P1's config.xml. Also Eclipse users need to add Xml / Java config in Spring Beans Support tab as well as enable support for import element.

这篇关于带有 JPA 的 Spring MVC:无法从 jar 实例化 JpaRepository的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆