Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default bean scope used by Spring Boot?

Tags:

spring-boot

I can't find this information anywhere. Can someone explain how spring boot 'decides' what the right scope is? Are the beans all singletons?

like image 441
Marco Lopez Avatar asked Sep 06 '15 05:09

Marco Lopez


People also ask

Why default bean scope is singleton in Spring?

When a bean is a singleton, only one shared instance of the bean will be managed and all requests for beans with an id or ids matching that bean definition will result in that one specific bean instance being returned. Only when you have to keep some session details you should use for example session scope.

What is the default scope of bean creation?

If it is already created, then the IOC container returns the same instance otherwise it creates a new instance of that bean only at the first request. By default, the scope of a bean is a singleton.

What is the use of @scope in Spring boot?

A scope defines the runtime context within which the bean instance is available. In Spring, a bean can be associated with the following scopes: Singleton. Prototype.

What is bean and scope of bean in Spring?

In Spring, bean scope is used to decide which type of bean instance should be returned from Spring container back to the caller. 5 types of bean scopes are supported : Singleton : It returns a single bean instance per Spring IoC container.


2 Answers

Spring Boot doesn't decide anything about the bean scope, this is plain Spring framework functionality. Default bean scope is singleton scope (meaning, one instance of that bean in the application).

like image 182
dunni Avatar answered Sep 20 '22 15:09

dunni


Here is the official documentation:

https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-scopes

Default scope for a Spring Bean in singleton.

like image 37
Gokhan Oner Avatar answered Sep 18 '22 15:09

Gokhan Oner