π Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
A simple example to demonstrates the difference between singleton scope and prototype scope with an example.
In this example, I have used a Java-based configuration using @Configuration, @Bean, and @Scope annotations.
Here’s an example to show you what’s the difference between bean scopes Singleton and Prototype. Let's create UserService class which is common for Singleton and Prototype scope examples.
In this example, I have used a Java-based configuration using @Configuration, @Bean, and @Scope annotations.
Here’s an example to show you what’s the difference between bean scopes Singleton and Prototype. Let's create UserService class which is common for Singleton and Prototype scope examples.
public class UserService {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Singleton scope example
If no bean scope is specified in bean configuration file, default to a singleton. Let's create the java based configuration file.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import com.javadevsguide.springframework.di.service.UserService;
@Configuration
public class AppConfiguration {
@Bean
// @Scope("singleton")
public UserService userService(){
return new UserService();
}
}
Note that @Scope("singleton") annotation is commented as Singleton Scope is the default.
Let's test this Singleton scope using application context standalone main() method.
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.javadevsguide.springframework.di.config.AppConfiguration;
import com.javadevsguide.springframework.di.service.UserService;
public class TestApplication {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
UserService userService = applicationContext.getBean(UserService.class);
userService.setName("Singleton scope test");
System.out.println(userService.getName());
UserService userService1 = applicationContext.getBean(UserService.class);
System.out.println(userService1.getName());
}
}
Output :
Singleton scope test
Singleton scope test
Since the bean ‘userService’ is in singleton scope, the second retrieval by ‘userService1’ will display the message set by ‘userService’ also, even it’s retrieve by a new getBean() method. In singleton, only a single instance per Spring IoC container, no matter how many time you retrieve it with getBean(), it will always return the same instance.
Prototype scope example
Define
@Scope("prototype") bean scope in bean java based configuration file. Let's create a java based configuration file.import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import com.javadevsguide.springframework.di.service.UserService;
@Configuration
public class AppConfiguration {
@Bean
@Scope("prototype")
public UserService userService(){
return new UserService();
}
}
Let's test this Prototype scope using application context standalone main() method.
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.javadevsguide.springframework.di.config.AppConfiguration;
import com.javadevsguide.springframework.di.service.UserService;
public class TestApplication {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
UserService userService = applicationContext.getBean(UserService.class);
userService.setName("Prototype scope test");
System.out.println(userService.getName());
UserService userService1 = applicationContext.getBean(UserService.class);
System.out.println(userService1.getName());
}
}
Output:
Prototype scope test
null
In the prototype scope, you will have a new instance for each getBean() method called. since the output is null in the second retrieval.
Bean scopes annotations on spring annotation based configuration
You can also use annotation to define your bean scope.
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("prototype")
public class CustomerService
{
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Enable auto component scanning
@Configuration
@ComponentScan("com.javadevsguide.springframework.di")
public class AppConfiguration {
}
Conclusion
In this example, we demonstrated the difference between singleton scope and prototype scope. Also in this example, we have used a Java-based configuration using @Configuration, @Bean, and @Scope annotations.The implementation of this simple Spring Java-based-configuration example can be found in the GitHub project – this is an Eclipse based project, so it should be easy to import and run as it is.
Github Repository: Singleton and Prototype Bean Scopes Examples
Spring Framework Related Posts
- Guide to Dependency Injection in Spring
- Spring Dependency Injection via Setter Example
- Spring Dependency Injection via Constructor Example
- Guide to Spring Bean Scopes
- Singleton and Prototype Bean Scopes Examples
- Spring @Qualifier Annotation Example
- Spring Java Based Configuration Basics
- Spring Java Based Configuration Example
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
π High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
π High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
π Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
π₯ Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
π₯ Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
π Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
π₯ Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
π Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
π₯ Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
π₯ Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
π₯ Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
π Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business

Comments
Post a Comment
Leave Comment