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
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();
}
}
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());
}
}
Singleton scope test
Singleton scope test
Prototype scope example
@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();
}
}
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());
}
}
Prototype scope test
null
Bean scopes annotations on spring annotation based configuration
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;
}
}
@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
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner

Comments
Post a Comment
Leave Comment