Java Spring Dependency Injection

Jul 4, 2024

C Snippet: Java Spring Dependency Injection

Agenda

  1. Prerequisites for understanding Dependency Injection
  2. Why Dependency Injection?
  3. What is Dependency Injection and its types?
    • Field Injection
    • Setter Injection
    • Constructor Injection
  4. Advantages and disadvantages of each type
  5. Common problems in Dependency Injection: Circular Dependency and Unsatisfied Dependency

Prerequisites

  • Previous video on the lifecycle of a bean, which covers the stages a bean goes through.
  • Injecting beans into constructed dependencies is one of these stages.

Why Dependency Injection?

Demonstration in Code

  • Example Setup: Spring Boot application with User class depending on Order class.
  • Problems: Direct instantiation (new Order()) causes tight coupling, problematic for future changes (e.g., converting Order to an interface).
    • Breaks Dependency Inversion Principle (part of SOLID): Should depend on abstractions, not concrete implementations.
    • Solution: Use Order interface with implementations OnlineOrder and OfflineOrder.

Solution with Dependency Injection

  • Framework: Spring's IoC (Inversion of Control) container handles dynamic dependency injection.
  • Annotations: Use @Component for classes and @Autowired for injecting dependencies.
  • Benefits: Adheres to Dependency Inversion Principle and facilitates future changes.

What is Dependency Injection?

  • Injecting dependencies at runtime as the application initializes.
  • Application Example: Order and User beans initialized by Spring at runtime.

Types of Dependency Injection

1. Field Injection

  • Inject dependencies directly into fields using @Autowired.
  • Advantages: Simple and easy to use.
  • Disadvantages: Cannot use with immutable fields (final), higher risk of NullPointerException.

2. Setter Injection

  • Inject dependencies via setter methods.
  • Advantages: Allows changing dependencies after object creation, useful for unit testing.
  • Disadvantages: Same issues with final fields, reduces readability and maintainability.
  • Implementation: Use @Autowired on setter methods.

3. Constructor Injection

  • Inject dependencies via the constructor.
  • Advantages:
    • Ensures all mandatory dependencies are injected upon initialization.
    • Avoids NullPointerException.
    • Supports immutable fields.
    • Fail-fast behavior makes missing dependencies apparent at compile time.
  • Implementation: Use @Autowired on constructors (optional if only one constructor).

Common Problems in Dependency Injection

1. Circular Dependency

  • Problem: Beans depend on each other, causing a cycle (Order depends on User and vice versa).
  • Solutions:
    • @Lazy annotation: Delays initialization until needed.
    • @PostConstruct annotation: Ensures beans are constructed before setting dependencies.

2. Unsatisfied Dependency

  • Problem: Multiple implementations for an interface lead to ambiguity in injection.
  • Solutions:
    • @Primary annotation: Marks one bean as the default choice.
    • @Qualifier annotation: Specifies which bean to inject.

Conclusion

  • Understanding dependency injection helps adhere to SOLID principles and facilitates future changes and testing.
  • Key annotations and concepts: @Component, @Autowired, @Lazy, @PostConstruct, @Primary, @Qualifier.
  • Ensures better software design and maintainability.

Thank you for watching! Don't forget to like, subscribe, and share this video with your friends!