Understanding Spring Bean Scopes: A Comprehensive Guide

Satyendra Jaiswal
3 min readJun 25, 2024

--

Spring Framework, one of the most popular Java frameworks, provides a powerful and flexible dependency injection mechanism. A crucial aspect of this mechanism is the concept of bean scopes. Bean scopes determine the lifecycle and visibility of the beans, dictating how many instances of a bean will be created and how they are shared. In this article, we will delve into the different bean scopes available in Spring and provide examples to illustrate their use.

What is a Spring Bean?

In Spring, a bean is an object that is instantiated, assembled, and managed by the Spring IoC (Inversion of Control) container. The configuration metadata, which you provide to the container, defines how these beans are created and managed.

Types of Bean Scopes

Spring provides several bean scopes, each serving different needs. The most commonly used scopes are:

  1. Singleton
  2. Prototype
  3. Request
  4. Session
  5. Global Session
  6. Application

1. Singleton Scope

The default scope in Spring, singleton, ensures that a single instance of a bean is created per Spring IoC container. This instance is shared across the entire container.

@Component
@Scope("singleton")
public class SingletonBean {
public SingletonBean() {
System.out.println("Singleton instance created");
}
}

2. Prototype Scope

In the prototype scope, a new instance of the bean is created every time it is requested from the Spring container.

@Component
@Scope("prototype")
public class PrototypeBean {
public PrototypeBean() {
System.out.println("Prototype instance created");
}
}

3. Request Scope

The request scope is specific to web-aware Spring applications. A new bean instance is created for each HTTP request.

@Component
@Scope("request")
public class RequestBean {
public RequestBean() {
System.out.println("Request instance created");
}
}

4. Session Scope

Similarly, the session scope is also used in web applications. A new bean instance is created for each HTTP session.

@Component
@Scope("session")
public class SessionBean {
public SessionBean() {
System.out.println("Session instance created");
}
}

5. Global Session Scope

The global session scope is used for applications that have portlet contexts. It creates a single bean instance per global HTTP session.

@Component
@Scope("globalSession")
public class GlobalSessionBean {
public GlobalSessionBean() {
System.out.println("Global session instance created");
}
}

6. Application Scope

The application scope is also specific to web applications. It creates a single bean instance for the lifecycle of a ServletContext.

@Component
@Scope("application")
public class ApplicationBean {
public ApplicationBean() {
System.out.println("Application instance created");
}
}

Example Application

To understand the practical implementation of these scopes, let’s consider a simple Spring Boot application.

Defining Beans

Define the beans with different scopes in your application.

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("singleton")
public class SingletonBean {
public SingletonBean() {
System.out.println("Singleton instance created");
}
}

@Component
@Scope("prototype")
public class PrototypeBean {
public PrototypeBean() {
System.out.println("Prototype instance created");
}
}

@Component
@Scope("request")
public class RequestBean {
public RequestBean() {
System.out.println("Request instance created");
}
}

@Component
@Scope("session")
public class SessionBean {
public SessionBean() {
System.out.println("Session instance created");
}
}

Using the Beans

Create a controller to demonstrate the usage of these beans.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BeanController {

@Autowired
private SingletonBean singletonBean;

@Autowired
private PrototypeBean prototypeBean;

@Autowired
private RequestBean requestBean;

@Autowired
private SessionBean sessionBean;

@GetMapping("/test")
public String testScopes() {
// Each request will demonstrate bean creation
return "Check the console output for bean instances creation.";
}
}

Running the Application

Run your Spring Boot application and access the /test endpoint multiple times. Observe the console output to see the instances being created.

Singleton instance created
Prototype instance created
Request instance created
Session instance created

Conclusion

Understanding and utilizing the correct bean scope is essential for designing efficient and scalable Spring applications. Each scope serves different purposes, and choosing the right one depends on your specific use case. Singleton and prototype scopes are commonly used in non-web applications, while request, session, global session, and application scopes are specific to web environments. By leveraging these scopes, you can ensure your Spring application manages beans effectively, optimizing performance and resource utilization.

Feel free to experiment with different scopes in your projects and observe how they influence the behavior of your application. Happy coding!

--

--