When to use constructor injection in Spring? [duplicate]

情到浓时终转凉″ 提交于 2020-08-21 18:37:05

问题


When to use constructor injection in Spring?

I heard that constructor injection is particularly useful when you absolutely must have an instance of the dependency class before your component is used. But what does it mean?

Can anybody explain me with some simple examples the following moments:

  • What are the benefits that I will get using constructor injection?
  • What is dynamic constructor injection?

回答1:


The correct approach is to use constructor injection whenever it's possible. Thanks to that it's clear what are the object dependencies and you are not able to create one if you do not provide all that are required.

Use constructor injection as below.

public class ProjectService {
    private final ProjectRepository projectRepository;
    private final TaskRepository taskRepository;

    public ProjectService(TaskRepository taskService, ProjectRepository projectService) {
        Assert.notNull(taskService);
        Assert.notNull(projectService);

        this.taskRepository = taskService;
        this.projectRepository = projectService;
    }

    public Project load(Long projectId) {
        return projectRepository.findById(projectId);
    }
}
  • final fields make sure that dependencies are not changed after object is initialized
  • Assert.notNull makes sure that you don't put null values instead of real objects.

When you use setter injection or field injection your API lets you create object in incorrect state. Consider an example:

public class ProjectService {
    private ProjectRepository projectRepository;
    private TaskRepository taskRepository;

    public Project load(Long projectId) {
        return projectRepository.findById(projectId);
    }

    @Autowired
    public void setProjectRepository(ProjectRepository projectRepository) {
        this.projectRepository = projectRepository;
    }

    @Autowired
    public void setTaskRepository(TaskRepository taskRepository) {
        this.taskRepository = taskRepository;
    }
}
ProjectService projectService = new ProjectService();
projectService.load(1L); // NullPointerException is thrown

For all not optional dependencies you can use setter injection. Read more on Oliver Gierke (Spring Data lead) blog: http://olivergierke.de/2013/11/why-field-injection-is-evil/

Update 15.01.2019

Starting from Spring 4.3 there is no need to put @Autowired annotation on the constructor if only single constructor is present.



来源:https://stackoverflow.com/questions/27132348/when-to-use-constructor-injection-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!