java - 如何正确地将许多服务注入(inject) Spring MVC Controller ?

标签 java spring model-view-controller dependency-injection controller

我创建了具有 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/

相关文章:

java - JTable TableRowSorter 或重新查询 MySQL DB?

security - 如何为 spring RestTemplate 设置可信证书

Spring Boot 安全 - Thymeleaf 秒 :authorize-url not working

spring - 如何仅使用注释使用 Spring 配置 Jersey

ruby-on-rails - 在MVC中,数据库是模型的一部分吗?

Node.js 和 Express.js 布局

java - Android 应用程序使用 AsyncTask 下载文件时出现内存泄漏

java - 安卓 "Could not find method"

java - 如何在不解压缩所有依赖项的情况下使用 Maven 组装控制台应用程序?

php - 我如何指定 Zend Framework 使用与 Action 默认文件不同的 View 文件?