小编Gio*_*Poe的帖子

对服务层中的存储库使用“autowired”注释和“private Final”有什么区别?

我看过一些教程,他们使用不同的语法来完成同样的事情。一旦创建学生对象的 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)

我读到它与构造函数和字段注入有关,但我真的不明白这种语法如何解决差异。有什么解释或资源可以让我更好地理解吗?先感谢您!

java annotations autowired spring-boot

3
推荐指数
1
解决办法
9782
查看次数

标签 统计

annotations ×1

autowired ×1

java ×1

spring-boot ×1