Spring Framework: Spring Bean scopes for your next interview – 2019

Spring is a powerful open source, application framework created to reduce the complexity of enterprise application development. It is light-weighted and loosely coupled. It has layered architecture, which allows you to select the components to use

Bean scopes is very common and important topic in spring framework, not only for interviews but to get an idea on how spring beans works in general.

There are 6 bean scopes designed by framework developers each for specific use.

1. Singleton bean scope

This is the default bean scope in spring framework, That means if you do not explicitly mention the scope type for a bean then spring will instantiate it as singleton.

A singleton bean means there will only one instance across the IOC container (Or usually across that JVM process). And it’ll be available across the IOC container so that you can autowire wherever you need it.

2. prototype bean scope

Prototype scope means that every time you ask spring (getBean or dependency injection) for an instance it will create a new instance and give a reference of that instance.

Note: If you are injecting a prototype scoped bean into a singleton class (ex. Controller) then it wont work, because Singleton beans will be instantiated only once when the spring container starts. And that’s when all the injections will happen including prototype scoped beans and the same references will be used until the singleton (Controller) bean destroyed.

3 request bean scope

Unlike prototype every injection creates new bean, Request scope creates new bean for each HTTP request. once the request is served then the bean will be destroyed.

Note: Request scope will be valid in the context of a web-aware Spring ApplicationContext only.

4 session bean scope

In session scope, container creates a new instance for each and every HTTP session. This scope will be used mostly when there is user session management module in the application.

So, if server has 20 active sessions (20 users logged in), then container can have at most 20 individual instances of bean class. All HTTP requests within single session lifetime will have access to same single bean instance in that session scope.

Any state change to one instance, will not be visible to other instances of that bean. These instances are destructed as soon as the session is destroyed/end on server.

Note: Only valid in the context of a web-aware Spring ApplicationContext.

5 application bean scope

In application scope, container creates one instance per web application run-time. It is almost similar to singleton scope, but that scope is limited to ServletContext only. whereas general singleton scoped bean is singleton per ApplicationContext.

Please note that there can be multiple application contexts for single application.

Note: Only valid in the context of a web-aware Spring ApplicationContext.

6 websocket bean scope

A single instance will be created and available during complete life-cycle of WebSocket.

In general, when a client initiates a websocket connection then a websocket scoped bean will be created and it will available throughout that websocket’s life-cycle.

Note: Only valid in web-aware Spring ApplicationContext.

Thanks for reading this article, If you find it helpful please let me know in the comments.

And If you want to contribute interview questions across any topic to questionsforinterview.in, please drop an email here kakurala@questionsforinterview.in

I’d be glad to hear from you.