【问题标题】:Spring MVC with JPA: Not able to instantiate JpaRepository from a jar带有 JPA 的 Spring MVC:无法从 jar 中实例化 JpaRepository
【发布时间】:2017-04-09 01:19:53
【问题描述】:

我正在尝试将标准 Spring 项目 P1 导入到 Web 项目 P2 中,并且我正在 P2 中实现 P1 的 Web 方面。 我已将 P2 中 P1 的所有资源作为 P1.jar 导入 还使用<import-resource> 显式导入了应用程序上下文文件,这成功发生了。 但是 JpaRepositories 不会在 P2 中自动装配。它似乎不在 P2 的上下文中。

谁能建议我在这里可能缺少的东西。

更新:2016 年 11 月 25 日

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扫描包)

<?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 的应用程序上下文中包含 &lt;jpa:repositories base-package&gt; 配置后,我不再遇到我之前遇到的奇怪错误。 此外,在 Spring bean 支持中启用 xml 配置的成功导入,其他一切都在适当的位置。只有 P1 中引用的 JpaRepositories 没有反映在 P2 的上下文中。

【问题讨论】:

  • 您是否正确配置了组件扫描?
  • 你能发布你的配置吗
  • 我已经更新了配置。

标签: java spring spring-mvc repository spring-data-jpa


【解决方案1】:

我认为您尝试做的事情是不可能的。您需要在 p2 中启用存储库扫描并在该扫描中提及包。

如果您的 p1.jar 在 p2 的类路径中,那么您只需在 p2 中启用存储库扫描,

<jpa:repositories base-package="com.home.p1.blog.repo" />

【讨论】:

  • 感谢您的信息。你能帮助我理解,如果 P1.jar 在 P2 的类路径中,它能够成功捕获 P1 的一个 bean(authorDAO)。如果我在jpa:repositoriesbase-package 属性中提供通配符,那么在类路径jar 中扫描jpa:repositories 在功能上是否受到限制? &lt;jpa:repositories base-package="com.home.*" /&gt;
  • 不,您当前上下文可访问的所有包都可以在您的基本包中指定。因此,您的项目 p2 的包以及类路径 jar 中的包(如 p1)都有资格被扫描。您只需要提及软件包即可。
【解决方案2】:

已解决:请注意,要从 P2 中的 P1.jar 导入 JpaRepositories,我们不必在 P2 的配置 xml 中提及 &lt;jpa:repositories&gt; 标签。 只要确保我们正确地导入了 P1 的 config.xml。 此外,Eclipse 用户需要在 Spring Beans Support 选项卡中添加 Xml / Java 配置,并启用对导入元素的支持。

【讨论】:

    猜你喜欢
    • 2017-04-19
    • 2018-08-20
    • 2017-04-27
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多