Card 06/ 07

ExerciseDifficulty: Advanced1 min

Assign the Right Scope

Three beans, three jobs. Assign singleton or prototype to each, and say whether wiring it into a singleton would need a scoped proxy.

  1. RideRepository — stateless, talks to the database, shared by everything.
  2. PdfReceiptBuilder — accumulates lines of text across several method calls while one receipt is being built, then is discarded.
  3. CurrentRideContext — one per web request, injected into a singleton RideAuditor elsewhere in the application.
See the assignments
  1. Singleton. No mutable per-caller state — the exact case singleton exists for.
  2. Prototype. It accumulates state across calls for one use and is then thrown away; sharing one across every receipt would replay the singleton-counter bug from earlier in this topic.
  3. Request-scoped, and yes — a scoped proxy, if anything wires it into a singleton the way RideAuditor did. Without the proxy, the singleton resolves it once at startup and never again.