Types of Dependency Injection
Last Updated :
01 Jul, 2025
Dependency Injection (DI) is a design pattern in Java that promotes loose coupling between components. In Java, mainly in frameworks like Spring, Spring Boot. And it is also used to remove hard-coded dependencies and make applications simple to manage and run.
In simple words, dependency injection means we can inject the dependency of one class into another class.
Types of Dependency Injection: There are three types of Dependency Injection:
- Constructor Injection
- Setter Injection
- Field Injection
Let's discuss one-on-one
1. Constructor Injection
When we inject the dependency of one class into another class via the constructor, then it is called constructor injection. Like we have taken the same example of a college and its departments
Benefits of using Constructor Injection
- Make your dependencies immutable (final).
- Make cleaner and testable code.
Example:
Java
//department class
class Department {
// delclare department name
private String Dname;
// Declaring a constructer
public Department(String Dname) {
this.Dname = Dname;
}
// method for return the department details.
public String getDetails() {
return "Department Details " + Dname;
}
}
// college class
class College {
// Inject department class reference
private Department department;
// 1. Constructor Injection
public College(Department department) {
this.department = department;
}
// Print department details.
public void showDepartment() {
System.out.println(department.getDetails());
}
}
// main class
public class Test {
public static void main(String[] args) {
// create a department class object
Department cse = new Department("Computer Science");
// inject into college class
College college1 = new College(cse);
college1.showDepartment();
// create second object of department class
Department Machenical = new Department("Machenical");
// Inject into college class
College college2 = new College(Machenical);
college2.showDepartment();
}
}
OutputDepartment Details Computer Science
Department Details Machenical
Explanation of code
- Department class stores the department name and returns the department details.
- College class depends on the Department and receives it via constructor injection.
- In the main method, two Department objects ("Computer Science" and "Mechanical") are created.
- Each Department is passed to a new College object through the constructor.
- showDepartment() prints the details of the injected department.
2. Setter injection
When we inject the dependency of one class into another class via the setter method, then it is called setter injection. Like we have taken the same example of a college and its departments.
Benefits of using Setter Injection
- Flexible to use, it can be changed later.
- More useful for dependency.
Example:
Java
/*package whatever //do not write package name here */
// Department class
class Department {
private String Dname;
public Department(String Dname) {
this.Dname = Dname;
}
public String getDetails() {
return "Department Details: " + Dname;
}
}
// College class using Setter Injection
class College {
private Department department;
// Setter method for injecting Department
public void setDepartment(Department department) {
this.department = department;
}
public void showDepartment() {
System.out.println(department.getDetails());
}
}
// Main class
public class Test {
public static void main(String[] args) {
// Creating department objects
Department cse = new Department("Computer Science");
Department mech = new Department("Mechanical");
// Creating College object and injecting department via setter method
College college1 = new College();
college1.setDepartment(cse);
college1.showDepartment();
// Injecting another department via setter
College college2 = new College();
college2.setDepartment(mech);
college2.showDepartment();
}
}
OutputDepartment Details: Computer Science
Department Details: Mechanical
Explanation of code
- Department class that holds the department name and returns its details.
- The College class uses setter injection to receive a Department object.
- In the main method, two Department objects are created: "Computer Science" and "Mechanical".
- Two College objects are created, and each is injected with a different Department via setDepartment().
- showDepartment() prints the details of the injected department by setter injection
3. Field injection
Dependencies are injected directly into the field by using the @Autowired or manually. This is nothing but field injection. This is fast but not recommended for testing.
Benefits of using field Injection
- It reduces boilerplate code.
- It is easy to use.
Example:
Java
// Department class
class Department {
private String Dname;
public Department(String Dname) {
this.Dname = Dname;
}
public String getDetails() {
return "Department Details: " + Dname;
}
}
// College class using Field Injection
class College {
// Field injection
public Department department;
public void showDepartment() {
System.out.println(department.getDetails());
}
}
// main class
public class Test {
public static void main(String[] args) {
// Creating department class objects
Department cse = new Department("Computer Science");
// Creating College object
College college = new College();
// Injecting dependency directly into the field
college.department = cse;
// show department
college.showDepartment();
}
}
OutputDepartment Details: Computer Science
Explanation of code
- Department class holds the department name and returns department details.
- A college class has a public field (department) for direct access — this is field injection.
- In the main(), a Department object ("Computer Science") is created.
- The department field of the College object is assigned directly (no constructor or setter used).
- showDepartment() prints the details of the injected department.
When to Use What?
1. Constructor Injection: When we use Constructor Injection, dependency when a dependency is mandatory, immutable, or needed for testing.
2. Setter Injection: Setter method dependency is used when the dependency is optional or requires late initialization
3. Field Injection: Field injection dependency is used for small projects, but not for production code.
Comparison Table:
Type | Flexibility | Readablity | Testability |
---|
Constructer Injection | Medium | High | High |
---|
Setter Injection | High | Medium | Medium |
---|
Field Injection | Low | Low | Low |
---|
Similar Reads
private vs private-final injection of dependency Dependency Injection (DI) is essential for improving code modularity, maintainability, and testability in the context of sophisticated Java programming. Choosing between private and private-final injection for dependencies is a typical DI concern. We will examine the distinctions between these two s
5 min read
Spring - Dependency Injection by Setter Method Dependency Injection is one of the core features of the Spring Framework Inversion of Control (IOC) container. It reduces the need for classes to create their own objects by allowing the Spring IOC container to do it for them. This approach makes the code more flexible, easier to test, and simpler t
5 min read
Spring - Setter Injection with Dependent Object Dependency Injection is the main functionality provided by Spring IOC(Inversion of Control). The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods. In Setter Dependency Injection(SDI) the dependency will be injected with the help of setters and
3 min read
Spring - Dependency Injection with Factory Method Spring framework provides Dependency Injection to remove the conventional dependency relationship between objects. To inject dependencies using the factory method, we will use two attributes factory-method and factory-bean of bean elements.Note: Factory methods are those methods that return the inst
8 min read
Dependency Injection with Dagger 2 in Android If there are two classes, class A and class B and class A depends on class B then class B is called dependent for class A.So, Every time we want to access class B in class A we need to create an instance of class B in Class A or use static factory methods to access class A. But this will make our co
7 min read
Resource Injection in Java EE The concept of resource injection can be used to inject any resource available in the JNDI (Java Naming and Directory Interface) namespace into any container-managed object, such as a servlet, a bean, etc. For example, resource injection can be used to inject data sources, connectors (such as databa
3 min read