Java Spring - 无法注入存储库

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

我正在尝试使用控制器、实体、服务和存储库创建一个基本的 Spring 应用程序。

我在将存储库注入我的服务时遇到了麻烦。 (至少我是这么想的)

我的员工实体:

@Entity
@Table
public class Staff {

    @Id
    @SequenceGenerator(
        name = "staff_sequence",
        sequenceName = "staff_sequence",
        allocationSize = 1
    )
    @GeneratedValue(
        strategy = GenerationType.SEQUENCE,
        generator = "staff_sequence"
    )
    private Long id;
    private String name;
    private String email;
    private String number;
    private LocalDate birthday;
    private Integer age;
    private String job;

    public Staff(){

    }

    public Staff(Long id, String name, String email, String number, LocalDate birthday, Integer age, String job){
        this.id = id;
        this.name = name;
        this.email = email;
        this.number = number;
        this.birthday = birthday;
        this.age = age;
        this.job = job;
    }

    public Staff(String name, String email, String number, LocalDate birthday, Integer age, String job){
        this.name = name;
        this.email = email;
        this.number = number;
        this.birthday = birthday;
        this.age = age;
        this.job = job;
    }

//Getter and Setter 

我的员工控制器:

@RestController
@RequestMapping(path = "api/v1/staff")
public class StaffController {

    private final StaffService staffService;

    public StaffController(StaffService staffService){
        this.staffService = staffService;
    }

    @GetMapping
    public List<Staff> getStaff(){
        return staffService.getStaffs();
    }
    
}

我的员工服务

@Service
public class StaffService {
    
    private final StaffRepository staffRepository;


        @Autowired
    public StaffService(StaffRepository staffRepository){
        this.staffRepository = staffRepository;
    }

    public List<Staff> getStaffs() {
        return staffRepository.findAll();
    }

}

还有我的员工存储库

@Repository
public interface StaffRepository extends JpaRepository<Staff, Long>{}

我的 IDE 告诉我 StaffService 类中的 @Autowired 是不必要的。但我正在观看的教程就是这样的。我从另一篇文章中找到了答案,也是以同样的方式完成的。

我尝试删除@Autowired,但它在其他位置。但公平地说,我不知道我在做什么:D

java spring-boot spring-data-jpa spring-data spring-repositories
1个回答
-1
投票

你需要在构造函数上方添加@Autowired,让spring知道应该使用注入

@RestController
@RequestMapping(path = "api/v1/staff")
public class StaffController {

    private final StaffService staffService;

    @Autowired
    public StaffController(StaffService staffService){
        this.staffService = staffService;
    }

    @GetMapping
    public List<Staff> getStaff(){
        return staffService.getStaffs();
    }
    
}

如果您在项目中使用lombok,则可以使用该代码获得相同的结果:

@RestController
@RequestMapping(path = "api/v1/staff")
@RequiredArgsConstructor
public class StaffController {

    private final StaffService staffService;

    @GetMapping
    public List<Staff> getStaff(){
        return staffService.getStaffs();
    }
}

@RequiredArgsConstructor 是来自 lombok 的注释。它为类中的所有最终字段生成构造函数,并且如果在此类类型的 spring 项目中注册了 bean,则将其注入到这些变量中。 (例如,您的服务是因为您添加了@service注释而注册的)

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