Bean Scopes in Spring Framework Explained with Examples

Spring Framework manages objects known as beans through its IoC Container. By default, Spring creates only one instance of a bean, but there are several bean scopes available depending on application requirements.


Understanding Bean Scopes is extremely important for interviews because it helps developers understand object lifecycle, memory usage and application behaviour.


What is Bean Scope?

Bean Scope defines how many instances of a Spring Bean should be created and how long those instances remain available within the Spring Container.

Different applications require different bean behaviours. Some beans should have only one instance while others may require a new instance every time.


Available Bean Scopes in Spring

  • Singleton
  • Prototype
  • Request
  • Session
  • Application
  • WebSocket

Singleton Scope

Singleton is the default scope in Spring Framework. Only one bean instance is created for the entire Spring Container.



@Service
@Scope("singleton")
public class EmployeeService {

}

Even if multiple components request this bean, Spring returns the same instance.


Example


EmployeeService service1 =
    context.getBean(
        EmployeeService.class
    );

EmployeeService service2 =
    context.getBean(
        EmployeeService.class
    );

System.out.println(
    service1 == service2
);

Output


true

Both references point to the same bean instance.


Prototype Scope

Prototype scope creates a new bean instance every time the bean is requested.



@Service
@Scope("prototype")
public class EmployeeService {

}

Example


EmployeeService service1 =
    context.getBean(
        EmployeeService.class
    );

EmployeeService service2 =
    context.getBean(
        EmployeeService.class
    );

System.out.println(
    service1 == service2
);

Output


false

Spring creates a completely new object every time.


Request Scope

Request scope is used in web applications. A new bean instance is created for every HTTP request.



@Component
@RequestScope
public class UserRequestBean {

}

Each request receives its own bean instance.


Session Scope

Session scope creates one bean per user session.



@Component
@SessionScope
public class ShoppingCart {

}

The bean remains available during the user's session.


Application Scope

Application scope creates one bean per web application.



@Component
@ApplicationScope
public class AppConfiguration {

}

The bean is shared across the entire application.


WebSocket Scope

WebSocket scope creates one bean per WebSocket session.

This scope is primarily used in real-time applications and messaging systems.


Singleton vs Prototype

Feature Singleton Prototype
Instances One Multiple
Default Scope Yes No
Memory Usage Low Higher
Performance Better Slightly Lower
Use Case Services State-based Objects

Real World Examples

Singleton Beans

  • Service classes
  • Repository classes
  • Configuration classes
  • Utility components

Prototype Beans

  • Report generators
  • Temporary processing objects
  • State-specific business objects

Bean Scope and Bean Lifecycle

Singleton beans are fully managed throughout the application lifecycle.

Prototype beans are created by Spring, but destruction callbacks are not managed automatically.

This is a favourite advanced interview question.


Common Interview Questions

What is the default bean scope in Spring?

Singleton.


Which bean scope creates a new object every time?

Prototype scope.


Which scope is used for HTTP requests?

Request scope.


Which scope is used for user sessions?

Session scope.


Does Spring manage the complete lifecycle of Prototype beans?

No. Spring manages creation but not complete destruction lifecycle for Prototype beans.


Best Practices

  • Use Singleton scope for stateless services.
  • Use Prototype only when multiple instances are required.
  • Avoid storing request-specific data in Singleton beans.
  • Understand scope behaviour before designing application architecture.
  • Prefer Singleton scope whenever possible.

Frequently Asked Questions (FAQ)

Can a Singleton bean inject a Prototype bean?

Yes, but special handling may be required depending on the use case.


What is the most commonly used Spring bean scope?

Singleton scope.


Can Bean Scope affect application performance?

Yes. Prototype scope creates more objects and may increase memory usage.


Conclusion

Bean Scope is a fundamental Spring Framework concept that controls how bean instances are created and managed. Understanding Singleton, Prototype, Request, Session and Application scopes helps developers design efficient Spring applications and answer Spring interview questions confidently.


Keywords

Spring Bean Scope, Bean Scopes in Spring, Singleton Scope Spring, Prototype Scope Spring, Request Scope Spring, Session Scope Spring, Spring IoC Container, Spring Framework Tutorial, Spring Interview Questions, Spring Boot Bean Scope, Java Spring Tutorial, Spring Core Concepts, Spring Application Scope, Dependency Injection Spring, Spring Architecture



Bean Scopes in Spring Framework Explained with Examples Bean Scopes in Spring Framework Explained with Examples Reviewed by 13thGhost on 1:07 AM Rating: 5

No comments:

Recommended Posts

Powered by Blogger.