In Spring, every bean is a singleton by default, so you define a singleton bean by simply declaring it with @Component (or @Service, @Repository, @Controller) or with a @Bean method in a @Configuration class. The container creates one shared instance per ApplicationContext and returns it for every injection point.
You can state the scope explicitly with @Scope("singleton"), but that is the same as the default. Note that a Spring singleton is one instance per container, not one per JVM like the classic Gang of Four singleton pattern.
Any class picked up by component scanning is a singleton unless you change its scope:
Spring Boot scans the package of the @SpringBootApplication class and everything below it. Inject the bean through the constructor:
With a single constructor, @Autowired is optional. Constructor injection is preferred over field injection because dependencies are explicit, can be final, and are easy to supply in tests.
Use @Bean for classes you do not own, such as library clients, or when construction needs logic:
By default the bean name is the method name. Calling priceService() from another @Bean method in a @Configuration class still returns the shared instance, because Spring proxies the class. In a @Configuration(proxyBeanMethods = false) class or a plain @Component, such a call creates a new object, so take the dependency as a method parameter instead.
Use this only for documentation value; it changes nothing.
Both lookups return the same object. If you change the scope to prototype, the output becomes false.
Singletons are created eagerly at context startup, which surfaces wiring errors early. To delay creation until first use, add @Lazy. The container also calls lifecycle callbacks once per singleton: @PostConstruct after injection and @PreDestroy on shutdown. Both annotations come from jakarta.annotation in Spring 6 and later.
Because many threads share one instance, a singleton must not keep per-request mutable state in its fields. Keep singletons stateless, or use thread-safe types such as ConcurrentHashMap and AtomicInteger. If you need state per request or per user, use @RequestScope, @SessionScope or prototype beans, and inject them through a scoped proxy or ObjectProvider into the singleton.
No. The pattern guarantees one instance per class loader. Spring guarantees one instance per bean definition per container, so two beans of the same class with different names produce two objects.
singleton. Other scopes are prototype, and in web applications request, session, application and websocket.
Spring does not make them thread safe. You must design them to be stateless or synchronize access to shared state.
Set @Scope("prototype") on the bean. When you need a prototype inside a singleton, inject an ObjectProvider<T> and call getObject().
Spring beans are singletons by default, whether you declare them with a stereotype annotation or a @Bean method. Rely on constructor injection, keep singletons stateless, and use @Scope only when you need a different scope such as prototype or request.