小编Mak*_*shi的帖子

Spring Data Repository自动装配失败

我正在研究这个问题很长一段时间,并且无法让它发挥作用.希望你们中的一些人可以不遗余力地帮助我.

这个场景
我建立它使用Spring MVC的Web应用程序.这个webapp应该使用另一个包含持久层的Maven项目.持久层当前仅由服务和Spring Data存储库组成.持久层本身可以对抗本地MySQL实例.

我的问题
我对持久层进行了JUnit测试.测试加载并保存实体.这是通过服务处理的.两种操作都很好.

现在我在webapp的Maven项目中的JUnit测试中尝试相同的操作.我再次尝试保存并加载实体.但这次失败了.Spring声明服务中的Repository引用无法自动装配,因为没有适合的bean.

我尝试过多种方法来解决这个问题.例如,使用直接布线,给出限定符等.我的方法都没有奏效.我甚至构建了两个完整的新Maven项目,我简化了场景,但保留了必要的元素.在那里,它像一个魅力.我不明白我在这个上做错了什么,因为我调整了工作示例中的设置.


持久性项目 persistence.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:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-autowire="byName">

    <tx:annotation-driven transaction-manager="transactionManager" />
    <context:annotation-config />

    <context:component-scan base-package="com.company.dev.webapp" />
    <jpa:repositories base-package="com.company.dev.webapp.repository" query-lookup-strategy="create-if-not-found" />

    <bean id="exceptionPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <!-- Data Source -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/webapp" />
        <property name="username" value="root" />
        <property name="password" value="" />
        <property name="initialSize" value="20" />
    </bean>

    <!-- Hibernate -->
    <bean …
Run Code Online (Sandbox Code Playgroud)

java spring autowired

12
推荐指数
1
解决办法
3万
查看次数

标签 统计

autowired ×1

java ×1

spring ×1