Spring Bean Scopes: Singleton vs Prototype, One Shared Object or a Fresh One
Ask Spring for the same EmailNotifier twice and compare with ==. By default you get one shared object and 'Same object? true'. Add @Scope("prototype") and every getBean makes a new one.
What you will learn
- Explain what a bean's scope decides
- Prove with == that a default bean is a singleton
- Switch a bean to @Scope("prototype") and predict the output
- Know when each scope's object is created
Before you start
- Spring Component Scanning
- Java == versus equals
If you ask Spring for the same bean twice, do you get the same object back? That depends on the bean's scope, and one small program shows both cases.
A shared printer or a paper cup
A classroom has one printer. Emma prints on it, Jack prints on it, and nobody buys a new printer per page. The water cooler next to it has paper cups, and everyone takes a new cup each time. The printer is a singleton, Spring's default. The cup is a prototype.
The code
package org.studyeasy.blog;
import org.springframework.stereotype.Component;
@Component
public class EmailNotifier {
public EmailNotifier() {
System.out.println("EmailNotifier created");
}
public void send(String title) {
System.out.println("Email sent: " + title);
}
}
No scope is written, which means singleton. The print in the constructor is our counter: it fires each time an object is made. BlogConfig is the usual empty @Configuration + @ComponentScan class, and the scan finds just this one component.
package org.studyeasy.blog;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class BlogApp {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(BlogConfig.class);
EmailNotifier a = context.getBean(EmailNotifier.class);
EmailNotifier b = context.getBean(EmailNotifier.class);
System.out.println("Same object? " + (a == b));
context.close();
}
}
For objects, a == b is true only when both variables point at the very same object, so it tells us exactly what Spring handed out.
Default: one shared object
EmailNotifier created
Same object? true
Spring makes the singleton right away, when the context starts, so "EmailNotifier created" prints once before either getBean runs. Both calls hand back that one object.
Prototype: a fresh one each time
Change one line: put @Scope("prototype") (from org.springframework.context.annotation) above the class, next to @Component. Run the same main:
EmailNotifier created
EmailNotifier created
Same object? false
This time Spring makes nothing at startup. A prototype bean is created only when someone asks for it. The first getBean calls new EmailNotifier(), the second calls it again, and a and b end up as two different objects.
Side by side
| singleton | prototype | |
|---|---|---|
| How to get it | nothing to write | @Scope("prototype") |
| Objects made | one for the whole app | one per getBean |
| When | when the context starts | when someone asks |
| Our output | 1 created line, true | 2 created lines, false |
Common mistakes
- Testing the scope with
equals.EmailNotifierdoesn't override it, so it happens to behave like==here, but==is the honest test for "same object". - Storing per-user data in a field of a singleton. Every caller shares that one object, so they share the field too.
- Expecting a prototype's constructor to run at startup. It only runs when the bean is requested.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Why Spring in 2026: What the Container Does and What Spring Framework 7 Changed
Spring in one line: a container that creates your classes and hands each one what it needs. Plus the Spring Framework 7 baseline (JDK 17 to 25, Jakarta EE 11, Tomcat 11, JUnit 6), what got better than Spring 6, and a two-line version check.
A Plain Spring Project, No Boot: Your First Bean With spring-context
One dependency, one recipe, one getBean: a Maven project with spring-context 7.0.9, a Blog record, an AppConfig with @Bean, and a main that asks the container for the Blog. Then delete @Bean and meet NoSuchBeanDefinitionException.
Inversion of Control and Dependency Injection in Spring: One Program, Built Three Ways
A PostService needs a PostRepository. Build it three ways: the service calls new itself (tight coupling), main hands it in through the constructor (DI by hand), and the Spring container does the wiring from a @Configuration recipe.
Spring Component Scanning: @Component and @ComponentScan Instead of @Bean Methods
Instead of one @Bean method per class, put @Component on EmailNotifier and PostService and @ComponentScan on an empty BlogConfig. Spring walks the package, finds the stickers and builds the beans; main doesn't change.
