# DI

<br>

### What is Dependency Injection?

* A design pattern where object dependencies are injected by an external source (such as a Framework) rather than by the object itself
* A technique of `receiving injection from the outside` rather than creating required objects on its own
  * Injecting necessary resources from the outside is exactly what dependency injection is

<br>

### DI and IoC (Inversion of Control)

* Dependency Injection is one of the `outcomes of applying the IoC concept`
  * Injecting dependencies can be viewed as an IoC-based action, but IoC and dependency injection are not the same thing

<br>

### DI and DIP (Dependency Inversion Principle)

DI is merely one of the techniques for implementing DIP; they are not the same concept

<br>

### What is Dependency Injection in Spring?

* Spring reduces coupling between modules using the DI approach
* DI in Spring means that the `Spring IoC Container` creates `Bean` objects and `injects dependencies on behalf of` the developer as defined
  * Here, IoC stands for Inversion of Control, meaning that the Spring IoC Container takes over what the user used to do directly -- creating and managing objects

<br>

### How is it implemented in Spring?

* What the developer needs to do
  * Write Bean classes
  * Configure injection (describe in xml files or use @(annotations))

<br>

### Why should you use Dependency Injection?

* Using dependency injection reduces coupling
* It enables loose coupling, and in the process, the OCP principle can be applied
  * OCP (Open Closed Principle)
    * The principle that code should be open for extension and closed for modification
* Spring operates based on IoC
  * Using IoC enables `separation of logic execution and implementation`, making it easier to switch between implementations
  * Since the container manages the Bean lifecycle, the number of management points for the developer is reduced
  * The pattern used in IoC is DI

<br>

### DI Methods and Their Pros and Cons

Spring Framework supports Dependency Injection in 3 ways

#### Constructor Injection

> A method of listing fields that require dependency injection in the constructor; it is one of the recommended approaches

* `Pros`
  * Since dependencies are guaranteed to exist when the instance is created, the existence of dependencies is ensured and they can be defined as `immutable`
  * It forces the instance to not be created without required references
  * From Spring 4.3 and above, if there is `only one constructor`, @Autowired is not needed
    * `Implicit constructor auto-injection`
  * Circular dependencies can be detected
  * Dependency target fields can be declared as `final` immutable objects
  * Writing test cases is easy because dependencies can be injected through the constructor
* `Cons`
  * Unavoidable `circular references` are difficult to resolve with constructor injection
    * In such cases, other injection methods should be used, but it is important to avoid circular references in the first place

#### Setter Injection

> A method of injecting by declaring the @Autowired annotation on setter methods

* `Pros`
  * Used when dependencies are `optionally needed`
  * Can selectively split injection to avoid excessive complexity from listing all dependencies in the constructor
* `Points to note`
  * If a default value for the injected dependency cannot be defined, null values may exist
* `Cons`
  * The dependency injection target field cannot be declared as a final immutable object

#### Field Injection → Deprecated

> A method of injecting by declaring the @Autowired annotation on member fields

* `Pros`
  * Code becomes concise
* `Cons`
  * Dependency relationships are not visible, making them abstract, which can lead to complex dependency relationships
  * An `anti-pattern` that violates SRP (Single Responsibility Principle)
  * Dependency injection is not easy during unit testing
  * The dependency injection target field cannot be declared as final

Conclusion: Use constructor injection and setter injection appropriately depending on the situation!
