gpt4 book ai didi

java - 将带有命名查询的 Spring Data 存储库注入(inject)到服务中

转载 作者:行者123 更新时间:2023-12-02 11:57:33 26 4
gpt4 key购买 nike

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

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 注释来注释此类存储库。这是为什么?是否需要实现此接口(interface),或者 @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。

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

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

我错过了什么吗?

最佳答案

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

关于java - 将带有命名查询的 Spring Data 存储库注入(inject)到服务中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47481863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com