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

时间:2017-11-25 01:30:02

标签: 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);

}

这里,它应该继承CrudRepository中定义的所有常见crud操作,并且还公开findByLogin函数。 大多数示例(如果不是全部)不使用@Repository注释来注释此类存储库。这是为什么?是否需要实现此接口或@Query以某种方式在幕后进行?

以下是我目前的情况:

package br.com.portal.service;

public interface UserService {
    User findByLogin(String login);
}

@Service
public class UserServiceImpl implements UserService {

    private UserRepository repository;

    @Autowired
    public UserServiceImpl(UserRepository repository) {
        this.repository = repository;
    }

    User findByLogin(String login) {
        return repository.findByLogin(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>

我没有使用Spring Boot。

使用上面的信息,我们应该能够重现以下错误:

Error creating bean with name 'userServiceImpl' defined in file xxx: Unsatisfied dependency expressed through constructor parameter 0; nested exception is NoSuchBeanDefinitionException: No qualifying bean of type 'UserRepository'

我错过了什么吗?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您需要告诉spring扫描存储库接口以提供实现。使用XML,您可以通过添加命名空间来完成此操作:

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

beans标记,然后包含此行

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

内部。 See this answer for more context

您的界面上不需要@Repository注释。这基本上来自于扩展CrudRepository

答案 2 :(得分:-1)

使用UserRepository

@Repository界面添加注释