Card 01/ 07

ConceptDifficulty: Intermediate1 min

Singleton and Prototype: How Many Instances Exist

Every bean has a scope, and unless you say otherwise, it is singleton: the container creates exactly one instance and hands that same object out to everyone who asks for it, for as long as the application runs.

prototype scope is the alternative: a fresh instance every time the bean is requested. Nobody shares one, and nothing about them is coordinated with each other.

Most beans should be singletons, and most of the time that is exactly right — a RideService or a repository has no reason to exist twice. The cost shows up specifically when a singleton holds mutable state, which the next card demonstrates.