Skip to main content
InterviewHubPRO
Section Overview
Q39

Question 39 of 50

Part 3: Microservices, Distributed Systems & Observability (25 - 35)

78%
Spring Boot & Microservices (50 Qs)•Part 3: Microservices, Distributed Systems & Observability (25 - 35)

Prototype Beans Inside Singleton Beans: The Scoped Proxy Solution.

senior LevelMost AskedPrototypesSystem DesignMicroservices

In Simple Words: A @Scope("prototype") bean is supposed to create a new instance on every request. However, if injected into a default @Scope("singleton") service:

  • The singleton is only created once at startup, so the prototype bean is only injected once, acting like a singleton!
  • The Solution: Set @Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS). Spring injects a dynamic CGLIB proxy that fetches a brand-new prototype instance on every method invocation.