Spring Framework FAQ: Top Questions
37. What are scopes of Spring Beans?
Spring supports several bean scopes to define the lifecycle and visibility of a bean in the application context.
📘 Common Scopes:
singleton
– Default. Single instance per Spring container.prototype
– New instance every time it's requested.request
– Per HTTP request (web apps only).session
– Per HTTP session (web apps only).application
– Scoped to ServletContext.
📥 Example:
@Scope("prototype")
@Component
public class MyBean {
}
🏆 Expected Output:
Each call to getBean() returns a new instance of MyBean.
🛠️ Use Cases:
- Use prototype for stateful beans.
- Use request/session/application for web application state.