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:
Spring has two types of Dependency Injection : Constructor based Injection -When container call the constructor of the class. It should be used for mandatory dependencies.
Constructor injection (from the definition) does not allow you to create circular dependencies between beans. This limitation is actually an advantage of constructor injection - Spring can resolve circular dependencies when setter injection is used without you even noticing.
Setter Injection has upper hand over Constructor Injection in terms of readability. Since for configuring Spring we use XML files, readability is a much bigger concern.
Constructor Injection: State Safe. The object is instantiated to a full state or is not instantiated at all. Field Injection: Consumer uses no-argument constructor. There is no valid way to set state of the object.
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 initializedAssert.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With