Lesson 48: Intro to Spring Boot & Dependency Injection
18 Aug 2026 1 min Swarnil Singhai
Spring Boot wires up a Java web application with minimal configuration, and its core idea — dependency injection — means classes declare what they need instead of constructing it themselves.
Real-time example
A @RestController for orders never constructs its own OrderService — Spring injects it, making the controller trivially testable with a mock.
@RestController
@RequestMapping("/orders")
class OrderController {
private final OrderService service;
OrderController(OrderService service) { // injected by Spring
this.service = service;
}
@GetMapping("/{id}")
Order getOrder(@PathVariable String id) {
return service.findById(id);
}
}
What's happening
Spring scans for @RestController/@Service classes at startup and wires their constructor dependencies automatically — no new OrderService() anywhere.
Constructor injection (as above) is preferred over field injection — it makes dependencies explicit and required, not hidden.
Advertisement