WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Framework · Intermediate · question 22 of 100

Explain the different scopes of a Spring Bean.?

📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

In Spring, the scope of a bean defines the lifecycle of that bean object, and how many instance(s) of that object should be created by the container.

There are five standard bean scopes supported in Spring Framework:

1. Singleton: This is the default scope of a Spring Bean. When a bean is configured as a singleton, only one instance of the bean is created by the container and the same instance is returned to every request for that particular bean. If a singleton bean depends on other prototype beans, then new prototype instances will be created for each request.

2. Prototype: This scope defines that each time a bean is requested, a new instance of that bean is created. Prototype scoped beans are not cached by the container and a new instance is created each time it is requested.

3. Request: This scope of a bean is in request scope if it is to be instantiated once per HTTP request. This means that for each HTTP request, a new instance of the bean is created and maintained. This scope is typically used for web applications using Spring MVC.

4. Session: This scope of a bean is in session scope if it is to be instantiated once per HTTP session. This means that for each HTTP session, a new instance of the bean is created and maintained. This scope is typically used for web applications using Spring MVC, especially for handling user session-specific data.

5. Global Session: This scope of a bean is in global session scope if it is to be instantiated once per global HTTP session. This means that for each global HTTP session (used in a portlet context), a new instance of the bean is created and maintained. This scope is specific to portlet-based web applications using Spring.

There are also custom bean scopes that can be defined and used by developers as per their specific requirements. For example, the Spring Cloud Context may define additional scopes for beans that are specific to cloud-based deployments.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview — then scores it.
📞 Practice Spring Framework — free 15 min
📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All 100 Spring Framework questions · All topics