Bean Scopes: Singleton, Prototype, Request, Session, Application and Custom
Learn Bean Scopes: Singleton, Prototype, Request, Session, Application and Custom (Phase 3 · Spring Beans (Essential), Spring Core) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
Singleton and prototype • singleton (default): one shared instance per container (not per JVM and not the GoF singleton: per bean definition per context). Created eagerly at startup, cached, destroyed on shutdown. Must be thread-safe and stateless (or carefully synchronised): most services, repositories and controllers. • prototype : a new instance on every request to the container ( getBean or injection). Spring creates and wires it but does not manage its destruction (no @PreDestroy ). Use for stateful, short-lived objects (builders, command objects).