Video summary

#7 Autowire using Spring Boot

Main summary

Key takeaways

Technology

Tech Summary (Spring Boot / Spring Framework Auto-wiring)

Goal of the video

Explain auto-wiring as an additional layer on top of Spring Boot’s dependency injection, showing how Spring automatically creates and connects bean dependencies.

Example domain model

  • A Dev class represents an application developer.
  • A Laptop class provides methods like compile() (and optionally debug()).
  • Initially, calling laptop.compile() fails because the laptop field is null unless an object is created.

How Spring fixes null via auto-wiring

  1. Mark classes with @Component so Spring Boot creates beans for them.
    • Without @Component, Spring doesn’t manage/instantiate the Laptop.
  2. Use @Autowired in Dev to automatically wire the dependency.
    • When used on a field, this is field injection.
    • Running the app produces the same output as manually doing new Laptop(), but without using new.

Injection types covered

  • Field injection (@Autowired on a field)
    • Works when @Autowired is present.
    • Used in the tutorial for simplicity.
  • Constructor injection
    • Works and is described as the default/optional approach with constructors.
    • @Autowired may not be required if you use constructor injection properly.
    • Example behavior: pass Laptop into the constructor and assign it to the field.
  • Setter injection
    • A setter method exists (e.g., setLaptop(...)), but it does not auto-wire by default.
    • Requires @Autowired on the setter to work.

Recommendation mentioned: Constructor and setter injection are considered “better,” while field injection is discouraged (though used for simplicity in the tutorial).


How Spring chooses which bean to inject (by type)

Spring resolves dependencies by type, not by variable or name.

To improve loose coupling, the tutorial demonstrates interface-based wiring:

  • Introduces an interface (e.g., Computer) with compile().
  • Laptop implements Computer.
  • Dev depends on the Computer type, and Spring injects the implementation matching that type.

This mirrors real-world behavior: companies provide a computer (laptop/desktop) rather than a specific model.


Handling ambiguity: multiple implementations of the same interface

If both Laptop and Desktop implement the same interface (Computer), Spring finds multiple candidate beans.

Result: application startup fails with an error indicating a field requires a single bean, but two were found (bean ambiguity).

Solutions demonstrated

  • Remove one implementation
    • For example, delete Desktop or remove its @Component.
  • Use @Primary on one implementation
    • If both are marked @Primary, Spring errors with “more than one primary found.”
  • Use @Qualifier to specify the exact bean by bean name
    • Example: @Qualifier("laptop") to select the laptop bean.

Key takeaway of the tutorial

The video ties together:

  • Auto-wiring (@Autowired)
  • Injection styles (field vs constructor vs setter)
  • Loose coupling using interfaces
  • Bean selection rules by type
  • Resolving conflicts using @Primary and @Qualifier

Main speakers / sources

  • Source: The tutorial is presented by the video author/instructor.
  • No other named speakers or external sources are mentioned in the subtitles.

Original video