Skip to main content

Posts

Showing posts with the label Request Scope

🛒 Spring Bean Scopes – Explained with an E-Commerce Website

☕ Spring Bean Scopes – Explained Like You’re Shopping Online 🛒 In one of my interviews, I was asked a simple question: "What are the scopes in Spring and when will you use them?" I thought I knew it... until the interviewer confused me so much that I started mixing prototype with singleton and session with application 🤯. So here’s my real-time, e-commerce-style explanation of Spring bean scopes — with code, diagrams, and some fun analogies! 1️⃣ Singleton Scope – 📦 The Universal Product Definition: Spring creates only one instance of the bean for the entire application context. Every @Autowired injection gets the same object . 🛒 E-Commerce Example: Think of Amazon's Shipping Service . Every order uses the same logic, the same configuration — no need to create a new object for each request. @Component @Scope("singleton") // Default one , So you don't want to mention explicitly public class ShippingService { private String shipp...