Spring Prototype Bean Scope
Fast recall
Quick revision
Core answer
Whether you understand when singleton scope is inappropriate and prototype is necessary
Not knowing that Spring doesn't manage the destruction lifecycle of prototype beans — no @PreDestroy callback runs
Explain the prototype-in-singleton trap and all three fixes: ObjectProvider, @Lookup method injection, and ApplicationContext.getBean()
Interview-ready guide
Interview answer
Prototype scope changes how BeanFactory resolves a bean. Calling getBean twice returns two instances. But when Spring constructs a singleton and injects one prototype into its constructor, that dependency is resolved only during singleton creation, so the singleton keeps it. To request a fresh instance per operation, inject ObjectProvider
Spring Prototype Bean Scope
Direct Answer
Prototype scope tells Spring to create a new bean instance each time that bean is requested from the container. It does not mean a new object appears on every method call, and a prototype injected once into a singleton is still that same injected instance. Spring performs initialization callbacks but does not manage complete destruction of prototype instances.
Code Example
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Component
@Scope("prototype") // a new instance every time
public class InvoiceGenerator {
private final List<String> lineItems = new ArrayList<>();
private BigDecimal totalAmount = BigDecimal.ZERO;
public void addItem(String item, BigDecimal price) {
lineItems.add(item); // mutable state
totalAmount = totalAmount.add(price);
}
public byte[] generatePDF() {
// build PDF from lineItems and totalAmount
return new byte[0];
}
}
Common Mistakes & Pitfalls
- Injecting prototype into singleton via constructor: The most common mistake. Spring creates one prototype instance at startup and reuses it forever — you lose prototype behavior. Use ObjectProvider, @Lookup method injection, or getBean() to get a fresh instance each time.
- Expecting Spring to call @PreDestroy: Spring doesn't track prototype beans after creation, so @PreDestroy methods never run. If your prototype holds resources like file handles or connections, close them manually.
- Using prototype when you need request or session scope: Freshers sometimes use prototype for web-scoped data like user sessions. That's wrong — you'll get a new instance on every injection, not one per request or session.
Check your understanding
Answer these without looking back at the article.
- What is the prototype-in-singleton problem?
- What is ObjectProvider in Spring?
- Does Spring call @PreDestroy on prototype beans?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.