Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Spring Framework FAQ: Top Questions

4. What are the different scopes of Spring beans?

Spring provides various scopes to manage the lifecycle and visibility of beans in different contexts.

📘 Common Bean Scopes:

  • singleton (default): one shared instance.
  • prototype: new instance every time requested.
  • request: per HTTP request (web only).
  • session: per HTTP session (web only).

📥 Example:

@Scope("prototype")
@Component
public class MyPrototypeBean {
}

🏆 Expected Output:

Each request to container returns a new instance.

🛠️ Use Cases:

  • Singletons for shared resources like services.
  • Prototype for stateful or short-lived objects.