Spring Bean Scopes Explained: Singleton, Prototype, Request, Session

🚀 Spring Framework 🌱 | Day 4 Spring Bean Scopes Made Simple (with Real-Life Examples) Understanding bean scopes in Spring is very important for writing efficient applications. Let’s break it down in a simple way 👇 👉 1. Singleton (Default Scope) Only one object is created for the entire application. 🏠 Real-life example: Think of it like a TV in your home – everyone uses the same TV. In Spring, all requests use the same bean instance. 👉 2. Prototype A new object is created every time it is requested. ☕ Real-life example: Ordering coffee at a café – every time you order, you get a new cup. In Spring, each request gets a fresh bean. 👉 3. Request Scope (Web apps only) One object per HTTP request. 🧾 Real-life example: Filling a form online – each request has its own data. Once the request ends, the object is gone. 👉 4. Session Scope One object per user session. 🛒 Real-life example: Shopping cart in an e-commerce app – items stay until you logout or session expires. 💡 Quick Summary: 👉 Singleton → One shared object 👉 Prototype → New object every time 👉 Request → One per HTTP request 👉 Session → One per user session 🚀 Pro Tip: Use the right scope based on your use case to avoid performance and memory issues. #SpringFramework #Java #BackendDevelopment #InterviewPrep #SoftwareEngineering

  • diagram

To view or add a comment, sign in

Explore content categories