我创建了具有 3 种用户类型的 Spring MVC 应用程序。我为它们每个人创建了单独的 Controller 。现在,在每个中,我都必须注入(inject)服务类,所以我这样做了:
@Controller
@RequestMapping("teacher")
public class TeacherController {
@Autowired
private StudentService studentService;
@Autowired
private GradeService gradeService;
@Autowired
private SubjectService subjectService;
@Autowired
private StudentGroupService studentGroupService;
@Autowired
private NewsService newsService;
@GetMapping("/index")
public String indexPage(Model theModel) {
List<News> tempNewsList = newsService.getNews();
theModel.addAttribute("theNewList", tempNewsList);
return "teacher/index";
}
此代码使用字段注入(inject)。据我现在了解到,这是一个应该避免并用构造函数注入(inject)替换的解决方案。因此,我使用所有这些字段 Autowiring 了一个构造函数,如下所示:
@Autowired
public TeacherController(StudentService studentService, GradeService gradeService, SubjectService subjectService, StudentGroupService studentGroupService, NewsService newsService) {
this.studentService = studentService;
this.gradeService = gradeService;
this.subjectService = subjectService;
this.studentGroupService = studentGroupService;
this.newsService = newsService;
}
用如此简单的代码创建如此冗长的构造函数,这是一个好的解决方案吗?如果我的代码中有更多服务怎么办?这是否可以接受,或者在这种情况下我应该重构我的代码,例如将服务委托(delegate)给其他服务或创建更多 Controller ?
最佳答案
你自己回答得很好! Spring 在文档 here 中正是解决了这个问题。在标题为基于构造函数还是基于 setter 的 DI? 的框中:
The Spring team generally advocates constructor injection, as it lets you implement application components as immutable objects and ensures that required dependencies are not null. Furthermore, constructor-injected components are always returned to the client (calling) code in a fully initialized state. As a side note, a large number of constructor arguments is a bad code smell, implying that the class likely has too many responsibilities and should be refactored to better address proper separation of concerns.
也就是说,理想情况下您应该进行重构。使用过SOLID原则并思考“我正在创建的类(class)的一项工作是什么?”。
关于java - 如何正确地将许多服务注入(inject) Spring MVC Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54118790/