Singleton and Prototype Bean Scopes Examples

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.
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

Comments