com.example.demo.services.StudentServiceImpl 需要类型为“com.example.demo.repositories.StudentRepository”的 bean 无法解决

问题描述 投票:0回答:1

我收到一条错误消息“描述:

com.example.demo.services.StudentServiceImpl 中构造函数的参数 0 需要类型为“com.example.demo.repositories.StudentRepository”的 bean,但无法找到。

行动:

考虑在配置中定义“com.example.demo.repositories.StudentRepository”类型的 bean。 ”

我已尽我所能,但未能解决问题。谁能帮我解决这个问题吗?谢谢

package com.example.demo.services;

import org.springframework.stereotype.Service;

import com.example.demo.entities.Student;
import com.example.demo.repositories.StudentRepository;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
public class StudentServiceImpl implements StudentService{
    private final StudentRepository studentRepository; 
      
    public StudentServiceImpl( 
        StudentRepository studentRepository) 
    { 
        this.studentRepository = studentRepository; 
    } 
  
    @Override
    public Mono<Student> saveStudent(Student student) 
    { 
        return studentRepository.save(student); 
    } 
  
    @Override public Flux<Student> getAllStudents() 
    { 
        return studentRepository.findAll(); 
    } 
  
    @Override public Mono<Student> getStudentById(String id) 
    { 
        return studentRepository.findById(id); 
    } 
  
    @Override public Mono<Void> deleteStudent(String id) 
    { 
        return studentRepository.deleteById(id); 
    } 
}

学生服务Imp

package com.example.demo.services;

import org.springframework.stereotype.Service;

import com.example.demo.entities.Student;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
public interface StudentService {
    Mono<Student> saveStudent(Student student); 
    Flux<Student> getAllStudents(); 
    Mono<Student> getStudentById(String id); 
    Mono<Void> deleteStudent(String id); 
}

学生服务

package com.example.demo.repositories;

import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.entities.Student;

@Repository
public interface StudentRepository  extends ReactiveMongoRepository<Student, String>{

}

学生存储库

package com.example.demo.entities;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class Student {
    @Id private String id; 
    private String firstname; 
    private String lastname; 
  
    public Student(String id, String firstname, String lastname) 
    { 
        super(); 
        this.id = id; 
        this.firstname = firstname; 
        this.lastname = lastname; 
    } 
  
    public Student() 
    { 
        super(); 
        // TODO Auto-generated constructor stub 
    } 
  
    public String getId() { return id; } 
  
    public void setId(String id) { this.id = id; } 
  
    public String getFirstName() { return firstname; } 
  
    public void setFirstName(String firstname) { this.firstname = firstname; } 
  
    public String getLastName() { return lastname; } 
  
    public void setLastName(String lastname) { this.lastname = lastname; } 
}

学生

package com.example.demo.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entities.Student;
import com.example.demo.services.StudentService;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/api/students")
public class StudentController {
    private final StudentService studentService; 
      
    public StudentController(StudentService studentService) 
    { 
        this.studentService = studentService; 
    } 
  
    public Mono<Student> 
    saveStudent(@RequestBody Student student) 
    { 
        return studentService.saveStudent(student); 
    } 
  
    @GetMapping public Flux<Student> getAllStudents() 
    { 
        return studentService.getAllStudents(); 
    } 
  
    @GetMapping("/{id}") 
    public Mono<Student> 
    getStudentById(@PathVariable String id) 
    { 
        return studentService.getStudentById(id); 
    } 
    @PostMapping
    public Mono<Student> 
    createStudent(@RequestBody Student student) 
    { 
        return studentService.saveStudent(student); 
    } 
  
    @DeleteMapping("/{id}") 
    public Mono<Void> deleteStudent(@PathVariable String id) 
    { 
        return studentService.deleteStudent(id); 
    } 
}

学生控制器

java spring-boot spring-mvc spring-webflux reactive-programming
1个回答
0
投票

请确保SpringBoot正在扫描

StudentRepository
所在的包。例如,确保
main.java
文件位于所有组件之上的根包中

© www.soinside.com 2019 - 2024. All rights reserved.