π Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
π 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 (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
Comments
Post a Comment
Leave Comment