首页 > 解决方案 > 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?

标签: javaspring-bootspring-securityspring-data

解决方案


一个可能的原因是 spring 没有在接口之外创建 Spring Data Repository。

简而言之,spring 数据项目在运行时生成一个“代理”——一个接口的实现,它将包含使用数据库所需的所有方法。

为了使这成为可能,您应该为您的 DAO 启用此代理生成:

这可以通过以下方式完成:

@EnableJpaRepositories(basePackages = "com.springboot.reserving.member")

因此,请确保您在 Spring Boot 应用程序类上有此注释。


推荐阅读