The @Scope annotation controls how Spring creates and manages instances of a bean. It helps decide if a new object is made each time or if the same one is reused.
@Scope for bean scope in Spring Boot
@Scope("scopeName") public class BeanClass { // bean code }
The scopeName is usually a string like singleton or prototype.
Common scopes include singleton, prototype, request, and session.
@Scope("singleton") public class MyService { // shared instance }
@Scope("prototype") public class MyPrototypeBean { // new instance each time }
@Scope("request") public class MyRequestBean { // one instance per HTTP request }
@Scope("session") public class MySessionBean { // one instance per user session }
This Spring Boot app defines a MessagePrinter bean with prototype scope. When the app runs, it injects two instances of MessagePrinter. Because of the prototype scope, these two instances are different objects. The program prints their references to show they are not the same.
import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope("prototype") public class MessagePrinter { public void printMessage() { System.out.println("Hello from MessagePrinter instance: " + this); } } import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ScopeExampleApplication implements CommandLineRunner { @Autowired private MessagePrinter printer1; @Autowired private MessagePrinter printer2; public static void main(String[] args) { SpringApplication.run(ScopeExampleApplication.class, args); } @Override public void run(String... args) throws Exception { printer1.printMessage(); printer2.printMessage(); } }
Singleton scope is the default in Spring, meaning one shared instance.
Prototype scope creates a new instance every time the bean is requested.
Web scopes like request and session only work in web-aware Spring contexts.
@Scope controls how many instances of a bean Spring creates.
Use singleton for shared beans and prototype for new instances each time.
Web apps can use request and session scopes for per-request or per-user beans.
Practice
@Scope("prototype") annotation do in a Spring Boot bean?Solution
Step 1: Understand the prototype scope meaning
Theprototypescope means Spring creates a new bean instance each time it is requested from the container.Step 2: Compare with other scopes
Unlikesingletonwhich shares one instance,prototypealways returns a fresh object.Final Answer:
Creates a new instance of the bean every time it is requested -> Option DQuick Check:
@Scope("prototype") = new instance each request [OK]
- Confusing prototype with singleton scope
- Thinking prototype beans are shared
- Mixing prototype with web scopes like session or request
@Scope annotation in Spring Boot?Solution
Step 1: Recall default and explicit singleton scope
Singleton is the default scope in Spring, but explicitly it is declared as@Scope("singleton").Step 2: Verify other scopes
prototypecreates new instances,sessionandrequestare web scopes, not singleton.Final Answer:
@Scope("singleton") -> Option CQuick Check:
Singleton scope = @Scope("singleton") [OK]
- Using @Scope("prototype") for singleton beans
- Confusing web scopes with singleton
- Omitting @Scope and expecting prototype behavior
@Component
@Scope("request")
public class UserRequestBean { }What happens when this bean is injected into two different HTTP requests?
Solution
Step 1: Understand request scope behavior
Therequestscope means Spring creates a new bean instance for each HTTP request.Step 2: Apply to two different requests
Since the requests are different, each gets its own separate bean instance.Final Answer:
Each request gets a new bean instance -> Option AQuick Check:
@Scope("request") = new bean per HTTP request [OK]
- Assuming request scope shares bean across requests
- Confusing request scope with session scope
- Thinking request scope behaves like singleton
@Component
@Scope("session")
public class ShoppingCart { }What is the likely error if you try to inject this bean into a singleton scoped bean without special handling?
Solution
Step 1: Understand scope mismatch problem
Injecting a session-scoped bean into a singleton bean causes a scope mismatch because singleton lives longer than session beans.Step 2: Result of mismatch without proxy
Without proxy or special configuration, Spring throws a runtime error as it cannot inject a shorter-lived bean into a longer-lived one safely.Final Answer:
Runtime error due to incompatible scopes -> Option BQuick Check:
Session bean into singleton without proxy = runtime error [OK]
- Assuming injection always works regardless of scope
- Ignoring need for proxyMode in such injections
- Confusing compilation and runtime errors
@Scope annotation and configuration should you use?Solution
Step 1: Identify session scope for per-session bean
Thesessionscope creates one bean instance per HTTP session, which fits the requirement.Step 2: Use proxyMode for injection into singleton beans
AddingproxyMode = ScopedProxyMode.TARGET_CLASSallows safe injection into other beans by creating a proxy that resolves the correct session instance.Final Answer:
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) -> Option AQuick Check:
Session scope + proxyMode = new instance per session shared inside session [OK]
- Using prototype scope which creates new instance every injection
- Using singleton scope which shares one instance globally
- Using request scope which creates new instance per HTTP request
Ready to prove it?
Pass the test and this pattern moves unknown
