Open In App

Types of Dependency Injection

Last Updated : 01 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. Constructor Injection
  2. Setter Injection
  3. 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();

    }

}

Output
Department 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();

    }
}

Output
Department 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();
    }
}

Output
Department 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


Article Tags :
Practice Tags :

Similar Reads