Different scopes of Spring beans with examples .

Jahid Momin
3 min readJan 16, 2023

--

In Spring, beans can have different scopes, which determines how many instances of the bean are created and how they are shared among different parts of the application. The different scopes available in Spring are:

  1. singleton: This is the default scope and creates only one instance of the bean throughout the entire lifecycle of the application. All requests for the bean will return the same instance.

A Spring Application Context is an container that holds all the beans that are managed by Spring. When you create a new Application Context, it creates new instances of all singleton beans that are defined in that context. So, if you have multiple application contexts in a single JVM, each context will have its own instance of singleton beans.

So, the scope of a singleton bean is per Spring Application Context, not per JVM.

@Service
//default singleton scope is there
public class MySingletonBean {
//bean methods and properties
}

2. prototype: This scope creates a new instance of the bean for each request. This scope is useful for stateful beans.

@Service
@Scope("prototype")
public class MyPrototypeBean {
//bean methods and properties
}
//In this example, MyPrototypeBean is a prototype bean
// and a new instance of this bean will be created for each creation request.

3. request: This scope creates a new instance of the bean for each HTTP request. It is only valid in the context of a web-aware Spring ApplicationContext.

@Controller
@Scope("request")
public class MyRequestBean {
//bean methods and properties
}
//MyRequestBean is a request-scoped bean and a new instance of this bean will be created for each HTTP request

4. session: This scope creates a new instance of the bean for each HTTP session. It is only valid in the context of a web-aware Spring ApplicationContext.

//session-scoped bean and a new instance of
// this bean will be created for each HTTP session

5. global-session: This scope creates a new instance of the bean for each global HTTP session. It is only valid in the context of a web-aware Spring ApplicationContext.

6. application: This scope creates a single instance of the bean per Spring ApplicationContext.

7. websocket: This scope creates a new instance of the bean for each WebSocket session. It is only valid in the context of a web-aware Spring ApplicationContext.

The scope of a bean can be specified in the configuration file, usually in XML format, or using annotations such as @Scope("singleton") or @Scope("prototype"). It's important to choose the right scope for your application.

For a Spring Boot REST application, the most common scopes to use for beans are singleton and request.

In a REST application, it is common to use singleton scope for services and repositories, which handle the business logic and data access, and request scope for controllers, which handle the incoming HTTP requests and return the appropriate response.

Happy Learning . Thank You . Keep Following

--

--

Jahid Momin
Jahid Momin

Written by Jahid Momin

Team Lead | Sr Software Engineer | Spring boot | Microservices | JavaScript | CodeIgniter | HTML | CSS | ReactJS | NextJS | Youtuber | Writer

No responses yet