What is setter injection?

Dependency injection (DI) is a design pattern crucial in enhancing software applications’ modularity, maintainability, and testability. It is a technique where the dependencies of a class are injected from the outside rather than being created within the class itself. Setter injection is one of the approaches used in dependency injection, offering flexibility and ease of use in managing dependencies.

Setter injection

Setter injection is a dependency injection technique that involves injecting dependencies into a class through setter methods. This approach sets the dependencies through the class’s public setter methods after the object has been instantiated. This allows for dynamic modification of dependencies at runtime and is particularly useful when dealing with optional or mutable dependencies.

Working of a setter injection

In Java, the process involves creating a class with uninitialized dependencies, providing setter methods to externally set these dependencies, and then invoking these setter methods to inject the dependencies into the class instance after it has been created. Let’s look at each step in detail:

  • Class definition: Define your class with private variables representing the dependencies. These variables are initially left uninitialized.

public class ExampleService {
private DependencyService dependency;
// Constructor and other methods
}
  • Setter method: Create setter methods for each dependency within the class. These public methods allow external components to inject dependencies into the class.

public class ExampleService {
private DependencyService dependency;
// Constructor and other methods
public void setDependency(DependencyService dependency) {
this.dependency = dependency;
}
}
  • Injection process: Instantiate an object of the class without initializing its dependencies. Then, use the setter methods to inject the required dependencies from external components or a dependency injection container. Here, the setDependency method provides the DependencyService instance to the ExampleService object.

ExampleService exampleService = new ExampleService(); // Object created without dependency
DependencyService dependencyService = new DependencyService();
exampleService.setDependency(dependencyService); // Dependency injected using setter method

Let’s execute the code that demonstrates setter injection by creating an instance of ExampleService, and injecting a DependencyService instance through a setter method below:

// Dependency class
class DependencyService {
public void execute() {
System.out.println("Executing dependency service");
}
}
// Service class that depends on DependencyService
public class ExampleService {
private DependencyService dependency;
// Constructor and other methods
public ExampleService() {
// No dependency initialization in the constructor
}
// Setter method for dependency injection
public void setDependency(DependencyService dependency) {
this.dependency = dependency;
}
// Method to use the dependency
public void performService() {
if (dependency != null) {
dependency.execute();
} else {
System.out.println("Dependency not set");
}
}
public static void main(String[] args) {
ExampleService exampleService = new ExampleService(); // Object created without dependency
DependencyService dependencyService = new DependencyService();
exampleService.setDependency(dependencyService); // Dependency injected using setter method
// Using the service
exampleService.performService();
}
}

Code explanation

  • Lines 2–6: This is a simple class named DependencyService with one method execute(), which prints a message when called. This class will act as the dependency.

  • Lines 9–10: The ExampleService class depends on an instance of DependencyService, which is stored in the private variable dependency.

  • Lines 18–19: public void setDependency(DependencyService dependency) sets the dependency variable to the passed DependencyService instance.

  • Lines 31–38: This creates an instance of ExampleService without setting the dependency initially. Creates an instance of DependencyService. Injects the DependencyService instance into the ExampleService instance using the setDependency method. Calls performService() on the ExampleService instance, using the injected dependency to execute its service method.

Benefits of using setter injection

Let’s look at some reasons why one should use setter injection:

  • Flexibility: Setter injection provides flexibility by allowing the modification of dependencies at runtime. This is particularly beneficial when dealing with optional or configurable dependencies.

  • Readability: Code readability is enhanced as the dependencies are explicitly set through setter methods. This clarifies the required dependencies and allows easy understanding of the class’s dependencies.

  • Testing: Setter injection simplifies the testing process, as it becomes easier to replace real dependencies with mock or stub objects for testing purposes.

  • Optional dependencies: When dealing with optional dependencies, setter injection allows the application to gracefully handle scenarios where certain dependencies may or may not be present.

Conclusion

Setter injection is a valuable technique offering a straightforward and flexible approach to managing dependencies in software applications. By allowing external components to set dependencies dynamically through setter methods, code readability, testability, and adaptability are enhanced. This contributes to the overall robustness and maintainability of the software. Understanding and implementing setter injection can significantly improve the design and structure of applications, making them more modular and easier to maintain in the long run.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved