使用JPA的Spring MVC:无法从jar

时间:2016-11-24 16:49:22

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

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

任何人都可以提出我在这里可能缺少的建议。

更新: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(实际上命名为: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>

更新:28Nov2016 在P2的应用程序上下文中包含<jpa:repositories base-package>配置后,我不再得到我之前得到的错误错误。 此外,在Spring bean支持中成功导入xml配置,其他所有内容都适当地落实到位。只有P1中引用的JpaRepositories没有反映在P2的上下文中。

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

解决:请注意,要从P2中的P1.jar导入JpaRepositories,我们不必在P2的config xml中提及<jpa:repositories>标记。 确保我们正确地导入P1的config.xml。 Eclipse用户还需要在Spring Beans Support选项卡中添加Xml / Java配置,并启用对import元素的支持。 enter image description here