java - Field memberRepo in (...) required a bean of type that could not be found
问题描述
I'm working on a Spring Boot project. Implementing Back-End code with the data, I've got an error. Before working on the security, that is, when I've just done with the MemberRepository, MemberService, and MemberController, it worked well. After I worked on the security, that kind of error occurs.
I'm using IntelliJ as the IDE, and the methods were MySQL, Java, Spring Boot, Spring Security, and Maven. The OS is Mac.
This is a part of MemberRepository.java code:
package com.springboot.reserving.member;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.context.annotation.Bean;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
public interface MemberRepository extends CrudRepository<Member, Long> { ... }
This is a part of MemberService.java code:
package com.springboot.reserving.member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class MemberService {
@Autowired
MemberRepository memberRepository;
...
}
This is CustomUserDetailService.java code:
package com.springboot.reserving.member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class CustomUserDetailsService implements UserDetailsService {
@Autowired
MemberRepository memberRepo;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return Optional.ofNullable(memberRepo.read(username))
.filter(m -> m != null)
.map(m -> new SecurityMember(m)).get();
}
}
The error message was:
Description:
Field memberRepo in com.springboot.reserving.member.CustomUserDetailsService required a bean of type 'com.springboot.reserving.member.MemberRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.springboot.reserving.member.MemberRepository' in your configuration.
What should I do to fix this error?
解决方案
一个可能的原因是 spring 没有在接口之外创建 Spring Data Repository。
简而言之,spring 数据项目在运行时生成一个“代理”——一个接口的实现,它将包含使用数据库所需的所有方法。
为了使这成为可能,您应该为您的 DAO 启用此代理生成:
这可以通过以下方式完成:
@EnableJpaRepositories(basePackages = "com.springboot.reserving.member")
因此,请确保您在 Spring Boot 应用程序类上有此注释。
推荐阅读
- android - 处理相关维度值
- javascript - 为什么我的谷歌脚本中没有触发 onEdit 函数
- javascript - 显示要跨越的 ajax api(json 响应)值
- rabbitmq - 如何检查 RabbitMQ firehose 是否打开
- javascript - 从 ReactJS 调用 javascript 函数
- java - 我正在尝试从 api 获取数据,但它似乎无法正常工作
- string - 当我们连接两个字符串时会发生什么
- odoo-10 - 下载使用 python-docx 创建的文档
- python - 在 ctypes 加载共享库中卸载共享库
- jenkins - 有没有更好的方法来根据规则调整gerrit插入代码大小并排除文件路径?