When to use constructor injection in Spring?

7.1k views Asked by At

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

There are 1 answers

5
Maciej Walkowiak On BEST ANSWER

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.