Spring Boot services · Chapter 40 of 41

Interview: Spring core

By the end of this chapter you can explain and fix the classic traps of Spring's proxies and scopes, the ones interviewers love. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The interview
  • 5 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

Tapri is hiring, and Meera wants you on the interview panel. "First you sit the interview yourself," says Dev, the CTO. He opens a branch of Tapri Cloud with five bug reports on it: a batch that does not roll back, receipts that pile up, a cycle hidden with @Lazy, a printer question, and counters that share one cart. "Fix them, and tell me why they happened."

Each bug passes a code review, because the Java looks right. All five come from one fact: Spring hands you proxies and scopes, and a plain Java call does not go through either.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1 · interview question

    Interview: fix the transactional batch that does not roll back

    Make ImportService.importAll(batches) import every batch in its own transaction: a batch with a bad row is rolled back whole, the other batches are kept, and it returns how many batches were imported.

    With SBC (two good rows), NDLS (one good row and the bad row "NDLS,,1") and CSMT (one good row), importAll returns 2 and leaves 3 rows: SBC's two and CSMT's one, none of NDLS's. Be ready to explain why the annotation on importBatch did nothing.

  2. Task 2 · interview question

    Interview: fix the prototype bean that behaves like a singleton

    Make every POST /receipts build its receipt with a fresh ReceiptBuilder: ["Masala chai", "Bun maska"] answers lines ["Masala chai 40", "Bun maska 50"] and total 90, and a second receipt for ["Cutting chai"] answers only ["Cutting chai 25"] and total 25.

    ReceiptBuilder stays a prototype, and CounterController may not keep a ReceiptBuilder in any field. Be ready to explain why the prototype behaved like a singleton.

  3. Task 3 · interview question

    Interview: break the circular dependency without @Lazy

    Make StockService and BackorderService stop depending on each other through their constructors, with no @Lazy on any constructor parameter, while backorders still fill straight away when stock arrives, oldest first.

    Ordering 5 and then 2 of an item with no stock answers BACKORDERED twice; restock(item, 6) fills "5 x <item>" but not the 2, leaves 1 in stock, and an order of 1 is then PLACED. Be ready to explain why @Lazy is not a fix.

  4. Task 4 · interview question

    Interview: predict what proxyBeanMethods changes

    Without changing the beans in PrinterConfig, make PrinterConfig.predict() return your Prediction: how many different Printer objects there are among the kitchenPrinter bean and the printers the SBC and NDLS counters hold, and whether the two counters hold the very same Printer, first for the class as written (proxyBeanMethods = false), then for the same class with proxyBeanMethods = true.

    The Check starts both versions and says how many of your four answers are right, never which. Be ready to explain why.

  5. Task 5 · interview question

    Interview: give each counter session its own cart

    Give every HTTP session its own CounterCart, while CounterController stays a singleton: after SBC's session adds Masala chai and Bun maska and NDLS's session adds Cutting chai, GET /cart answers ["Masala chai", "Bun maska"] in SBC's session, ["Cutting chai"] in NDLS's, and [] in a new session.

    Be ready to explain how a singleton controller reaches a different cart in every session.

What you will learn

  • Self-invocation: a call inside a class never reaches its proxy.
  • Prototypes in singletons: a bean asked for once stays one bean.
  • Circular dependencies: why Spring refuses them, and how to remove one.
  • Full and lite configuration: what proxyBeanMethods changes.
  • Web scopes: one bean per HTTP session, through a scoped proxy.

More in The interview

Build "Interview: Spring core" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.