我看过一些教程,他们使用不同的语法来完成同样的事情。一旦创建学生对象的 POST 请求通过控制器传入,服务层就会使用这两种方法注入存储库。
方法一:
@Service
@AllArgsConstructor
@Transactional
public class StudentService {
private final StudentRepository studentRepo;
// complete service code using studentRepo
}
Run Code Online (Sandbox Code Playgroud)
以及方法2:
@Service
public class StudentService {
@Autowire
private StudentRepository studentRepo;
// complete service code using studentRepo
}
Run Code Online (Sandbox Code Playgroud)
我读到它与构造函数和字段注入有关,但我真的不明白这种语法如何解决差异。有什么解释或资源可以让我更好地理解吗?先感谢您!