Java 将带有命名查询的Spring数据存储库注入服务

Java 将带有命名查询的Spring数据存储库注入服务,java,dependency-injection,spring-data,spring-data-jpa,Java,Dependency Injection,Spring Data,Spring Data Jpa,我有以下存储库,就这么简单: package br.com.portal.repository; public interface UserRepository extends CrudRepository<User, Long> { @Query("SELECT u FROM User WHERE u.login = :login") User findByLogin(@Param("login") String login); } 以及spring-mvc

我有以下存储库,就这么简单:

package br.com.portal.repository;

public interface UserRepository extends CrudRepository<User, Long> {

    @Query("SELECT u FROM User WHERE u.login = :login")
    User findByLogin(@Param("login") String login);

}
以及spring-mvc.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- Defines the static resources location, otherwise resource requests will result in 404 errors (not found) -->
    <mvc:resources mapping="/assets/**" location="/assets/" order="0" cache-period="31556926" />
    <mvc:resources mapping="/favicon.ico" location="/assets/icon/favicon.ico" cache-period="31556926" />

    <!-- Defines the custom Spring components packages -->
    <context:component-scan base-package="br.com.portal.repository">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
    </context:component-scan>
    <context:component-scan base-package="br.com.portal.service">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>
    <context:component-scan base-package="br.com.portal.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!-- JPA -->
    <bean class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="default" />
    </bean>
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

</beans>
我不使用弹簧靴

根据当前的上述信息,我们应该能够重现以下错误:

创建文件xxx中定义的名为“userServiceImpl”的bean时出错:通过构造函数参数0表示的未满足的依赖关系;嵌套异常为NoSuchBeanDefinitionException:没有“UserRepository”类型的符合条件的bean


我遗漏了什么吗?

用@Repository注释UserRepository接口

Spring告诉您没有名为“userserviceinpl”的bean,这看起来是正确的。不存在区分大小写的文本。您应该了解如何命名bean。您可能只需要在@Service注释中提供一个名称。

您需要告诉spring扫描存储库接口以提供实现。使用XML,您可以通过添加名称空间来实现这一点:

xmlns:jpa="http://www.springframework.org/schema/data/jpa"
添加到bean标记,然后包括此行

<jpa:repositories base-package="br.com.portal.repository"/>
里面


界面上不需要@Repository注释。这基本上是通过扩展Crudepository来实现的。请确保您的回购将被扫描。如果我实现UserRepository,它可以工作,但随后它就失去了@Query和Crudepository的用途。看起来我必须使用而不是,但我也不能让它工作。这个bean是正确的。如果您阅读了我的评论,您会发现如果我实现UserRepository接口,它确实会起作用。这意味着Spring没有将该存储库视为一个。谢谢,@Jens。我本来想回答我自己的问题,但你先回答了。我花了一段时间才发现。另外,请更新您的答案,将Spring数据版本1.9.0.RELEASE包含在pom.xml中。最新版本抛出AbstractMethodError根据stackoverflow的回答,这与spring data jpa和spring data commons JAR的版本/依赖项之间的冲突有关。不确定您的意思。你和我都不是在引用pom,也似乎与问题无关。你有你提到的版本冲突讨论的链接吗?我有一个Maven项目,上面的示例(包括你的答案)只适用于SpringData1.9.0.RELEASE。如果我使用最新版本2.0.2.RELEASE,它会崩溃并出现以下错误:org.springframework.beans.factory.BeanCreationException:创建名为“userRepository”的bean时出错:bean初始化失败;嵌套异常为java.lang.AbstractMethodError。我再也找不到讨论了。混合来自不同发布序列的spring数据jpa和spring数据共享将导致问题。但SpringDataJPA应该,而且我非常肯定它确实带有适当的可传递依赖项。因此,这应该在2.x中也适用。如果没有,你总是可以提出另一个问题。确保包含完整的堆栈跟踪。spring-data-jpa随spring数据公用程序提供。我在pom.xml中只引用了spring数据jpa版本2.0.2.RELEASE,但它在启动应用程序时会导致上述异常。但是,如果版本更改为1.9.0.RELEASE,则一切正常。
<jpa:repositories base-package="br.com.portal.repository"/>