Skip to content
the poor coder

What are the types of Dependency Injection?

Poor Man
Published
Reading time
1 min read
On this page

Types of Dependency Injection#

Dependency Injection is a popular design pattern in software engineering that is used to reduce the coupling between different parts of an application. There are three types of dependency injection: Constructor Injection, Setter Injection, and Interface Injection.

1. Constructor Injection#

Constructor injection is the most common and simplest form of dependency injection. In this method, the dependencies are provided to the class through its constructor. The class that owns the dependencies constructs them by using a constructor parameter. This method ensures that the class has all its dependencies available when it is created.

Java

public class MyClass {
   private MyDependency myDependency;

   public MyClass(MyDependency myDependency) {
      this.myDependency = myDependency;
   }
}

2. Setter Injection#

Setter injection is another type of dependency injection. In this method, the dependencies are provided to the class through setter methods. The class that owns the dependencies provides them by invoking a setter method on the class. This method is more flexible than constructor injection because you can set the dependencies after the class has been created.

Java

public class MyClass {
   private MyDependency myDependency;

   public void setMyDependency(MyDependency myDependency) {
      this.myDependency = myDependency;
   }
}

3. Interface Injection#

Interface injection is less common than the other two methods of dependency injection. In this method, the dependent object implements an interface that defines methods to set the dependencies. The class that owns the dependencies provides them by invoking the methods defined in the interface. This method is more complex than the other two methods of dependency injection but can be useful in certain situations.

Java

public interface MyInterface {
   public void setMyDependency(MyDependency myDependency);
}

public class MyClass implements MyInterface {
   private MyDependency myDependency;

   public void setMyDependency(MyDependency myDependency) {
      this.myDependency = myDependency;
   }
}

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC