Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use constructor injection in Spring? [duplicate]

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?
like image 865
XYZ Avatar asked Nov 25 '14 16:11

XYZ


People also ask

When should we use constructor injection in Spring?

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.

What is the limitation of constructor injection in Spring?

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.

Which one is better setter or constructor injection?

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.

What is the difference between constructor injection and field injection?

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.


1 Answers

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.

like image 174
Maciej Walkowiak Avatar answered Sep 19 '22 10:09

Maciej Walkowiak