Search code examples
javaspringspring-bootspring-data-jpaspring-data

Service required a bean of type 'Repository' that could not be found in Springboot project


i am new to springboot. i try to run with my data using jpa i meet this problem. APPLICATION FAILED TO START


Description:

Parameter 0 of constructor in com.example.service.MemberService required a bean of type 'com.example.repository.MemberRepository' that could not be found.

Action:

Consider defining a bean of type 'com.example.repository.MemberRepository' in your configuration.

this is my project structure: enter image description here

MemberService.java

@Service
public class MemberService {

    private final MemberRepository memberRepository;

    @Autowired
    public MemberService(MemberRepository memberRepository) {

        this.memberRepository = memberRepository;
    }

    public List<Member> getAllMembers() {
        return memberRepository.findAll();
    }
}

MemberController.java

@Controller
public class MemberController {

    private final MemberService memberService;

    @Autowired
    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }

    @GetMapping("/members")
    public String getAllMembers(Model model) {
        List<Member> members = memberService.getAllMembers();
        model.addAttribute("members", members);
        return "members";
    }
}

MemberRepository.java

@Repository
public interface MemberRepository extends JpaRepository<Member, Long> {
}

Member.java


@Entity
public class Member {
    private Long id;
    private String name;
    private String kana;
    private LocalDate birthDate;
    private String birthPlace;
    private String ki;
    private String link;

    public void setId(Long id) {
        this.id = id;
    }

    @Id
    public Long getId() {
        return id;
    }

    // Getter and Setter methods
}

Main method

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        String serverPort = context.getEnvironment().getProperty("server.port");
        String contextPath = context.getEnvironment().getProperty("server.servlet.context-path", "");
        System.out.println("Application is running at: http://localhost:" + serverPort + contextPath);
    }

}

i check some stackoverflow questions like me but i don't find the right method to solve. i will be thankful if you give me the right solution and tell me how i change my code. thanks for you reply i really need your help.


Solution

  • persistence-api problem:

    Spring Boot error with a repository/entity, "not a managed type"

    and build a new project: https://start.spring.io/

    and check if dependency version is able to compatible.