Concept
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.
The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.
A few lines that show Dependency injection doing its job.
@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(); } }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.
Your roadmap puts these before Dependency injection, unless you already know them.
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.
Topics that build on Dependency injection.