Spring MVC with JPA: Not able to instantiate JpaRepository from a jar

ⅰ亾dé卋堺 提交于 2019-12-12 03:43:18

问题


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.

UPDATE: 25Nov2016

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

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.


回答1:


I dont think what you are trying to do is possible. You need to enable repository scan in your p2 and mention the packages in that scan.

If your p1.jar is in the classpath of p2 then you only have enable repository scan in your p2,

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



回答2:


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.



来源:https://stackoverflow.com/questions/40791448/spring-mvc-with-jpa-not-able-to-instantiate-jparepository-from-a-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!