1. Concept library
  2. Java & Spring Boot
  3. Module 6: Spring Boot fundamentals

Concept

Dependency injection

Spring creates your objects and passes their dependencies in through the constructor. Classes declare what they need rather than building it, which makes them easy to test and rewire.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what the container does when it builds your objects
  2. Write a service that takes its dependencies through the constructor
  3. Spot field injection and explain what it costs in tests

In practice

A few lines that show Dependency injection doing its job.

Java
@Service
public class InvoiceService {
    private final InvoiceRepository invoices;
    private final Clock clock;

    public InvoiceService(InvoiceRepository invoices, Clock clock) {   // constructor injection
        this.invoices = invoices;
        this.clock = clock;
    }
}

@Configuration
class TimeConfig { @Bean Clock clock() { return Clock.systemUTC(); } }

The common mistake

Watch for

Field injection with @Autowired on private fields. The class cannot be constructed without Spring, so unit tests need reflection or a container; constructors make dependencies explicit and final.

What to know first

Your roadmap puts these before Dependency injection, unless you already know them.

How it stays learned

Once a session shows you understand Dependency injection, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Dependency injection.

Previous in the module← Auto-configuration
Next in the moduleApplication properties →