Types of DI
In Spring Boot, Dependency Injection (DI) is a design pattern that allows the components of an application to be loosely coupled, making the code more modular and easier to maintain. Spring Boot supports several types of Dependency Injection. Here are the main types:
-
Constructor Injection:
- In this type, dependencies are injected through the constructor of the class.
- It is considered a best practice because it ensures that the required dependencies are provided at the time of object creation.
- Example:
@Service public class MyService { private final MyRepository myRepository; @Autowired public MyService(MyRepository myRepository) { this.myRepository = myRepository; } }
-
Setter Injection:
- Dependencies are injected using setter methods.
- This allows for flexibility as it is possible to change the dependencies at runtime.
- Example:
@Service public class MyService { private MyRepository myRepository; @Autowired public void setMyRepository(MyRepository myRepository) { this.myRepository = myRepository; } }
-
Field Injection:
- Dependencies are injected directly into the fields of the class.
- While convenient, it is often considered less preferable than constructor injection as it makes testing and mocking more difficult.
- Example:
@Service public class MyService { @Autowired private MyRepository myRepository; }
-
Method Injection:
- Dependencies are injected through a method.
- This is less common than constructor or setter injection.
- Example:
@Service public class MyService { private MyRepository myRepository; @Autowired public void injectDependency(MyRepository myRepository) { this.myRepository = myRepository; } }
Constructor Based Dependency Injection is Recommended
• All dependencies are available at initialization time.
• Immutability and avoid NullPointerException.
• Preventing errors in Tests.