Showing posts with label Bean Scopes. Show all posts
Showing posts with label Bean Scopes. Show all posts

Wednesday, April 22, 2020

Singleton Scope Examples in Spring Boot

1. Introduction


In this article, We'll be learning how to create a singleton bean in Spring Boot and Spring applications. Once a bean is declared as singleton then spring container will create exactly only one instance for that bean. But, We can make bean scope as singleton as a result container will create only once instance as similar to the Singleton Desing Pattern.

By Default, A bean in spring is treated as singleton scoped. But, This can be changed to prototype as well with <bean> tag attribute and @Scope annotation.

Spring Boot Singleton Scope Examples

2. Singleton With @Scope Annotation


First of all, we will see now how to create and next, how to make bean as a singleton with @Scope annotation. @Scope Annotation has attribute value that takes the value as ConfigurableBeanFactory.SCOPE_SINGLETON or ConfigurableBeanFactory.SCOPE_PROTOTYPE

Another this is we already discussed in the previous article, Bean creation with @Bean annotation.