Discover how a simple annotation can save you from tangled object management headaches!
Why @Scope for bean scope in Spring Boot? - Purpose & Use Cases
Imagine you have a service in your app that creates a new object every time you ask for it, but you have to write code to manage when to create it and when to reuse it.
You manually track instances everywhere, which quickly becomes confusing and messy.
Manually managing object lifecycles is error-prone and leads to bugs like creating too many objects or sharing objects when you shouldn't.
This makes your code hard to read, test, and maintain.
The @Scope annotation in Spring Boot lets the framework handle how many instances of a bean exist and when they are created.
This means you just declare the scope once, and Spring manages the rest automatically.
MyService service = new MyService(); // manually new every time
// or reuse global instance manually@Service @Scope("prototype") public class MyService { } // Spring creates new instance every time it's requested
You can easily control bean lifecycles, making your app more efficient and your code cleaner without extra manual work.
In a web app, you want one shared service for all users (singleton scope), but a new object every time it's requested (prototype scope). @Scope lets you do this simply.
Manually managing object creation is complex and error-prone.
@Scope lets Spring control bean lifecycles automatically.
This leads to cleaner, safer, and more maintainable code.
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
