Skip to content

Dependency Injection (DI) - Managing Dependencies Effectively

Dependency Injection (DI) is a design pattern and a form of Inversion of Control (IoC) where an object receives its dependencies from an external source rather than creating them itself. DI promotes loose coupling, testability, and modularity.


Constructor Injection

Dependencies are provided through a class constructor.

Setter Injection

Dependencies are provided through setter methods.

Interface Injection

Dependencies are provided through an interface method.


  1. Define Dependencies: Identify the dependencies a class needs.
  2. Inject Dependencies: Provide those dependencies from an external source (e.g., a DI container).
  3. Use Dependencies: The class uses the injected dependencies without knowing how they are created.

// Constructor Injection
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
// Setter Injection
@Service
public class UserService {
private UserRepository userRepository;
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
}



ProsCons
Reduces coupling between componentsCan introduce complexity if overused
Makes applications more testable and modularRequires learning a DI framework or container
Easier to swap implementationsCan lead to “magic” behavior if not used carefully

  • Refactor a small project to use DI and observe the benefits.
  • Experiment with different types of DI (constructor, setter, interface).