World 1 / Quest 1.3

Constructor Injection and the Wiring Test

Wire controller to service to repository without hiding dependencies.

Concept

Constructor injection makes dependencies boring and visible. That is the point. The code should tell you what a class needs before Spring ever enters the room.

Task

  1. Create a cinema controller under /api/v1/cinemas.
  2. Create a service class and an in-memory repository for now.
  3. Use constructor injection only.
  4. Add a SpringBootTest that proves the controller, service, and repository all load.

Run

./gradlew test --tests "*CinemaWiringTest"

Expected Result

  • The wiring test passes.
  • A search for field @Autowired returns no production matches.

Common Traps

  • Putting business rules in the controller.
  • Letting the controller talk directly to the repository.
  • Using field injection because older tutorials did.

Hint Ladder

Hint 1

Ask who owns behavior. The controller owns HTTP. The service owns use-case decisions.

Hint 2

The repository can be a simple component until Phase 2 creates the database.

Hint 3

If a dependency is required, make it a final field and constructor parameter.

Solution

Reference solution will link to the phase-1-done tag once the learner repo is public.

Boot 3 to 4 Delta

Constructor injection is not new. The interesting Boot 4 note is what the starter did and did not auto-configure.