Understanding and Configuring Bean Scope in Spring Framework

The Spring Framework provides a comprehensive approach to building enterprise-ready applications with ease. One of its key features is the ability to manage and control the lifecycle and scope of beans within the application context. In this article, we will dive into the concept of bean scope and explore the various options available in Spring, including singleton, prototype, request, session, and more.

Singleton Scope

Singleton is the default bean scope in Spring. When a bean is defined with singleton scope, Spring Container creates and maintains a single shared instance of that bean throughout the application context. Any subsequent requests for the same bean will result in returning the same single instance.

To define a bean with singleton scope in Spring, you can simply omit the @Scope annotation or explicitly declare it as @Scope("singleton"). For example:

@Component
public class MySingletonBean {
    // class implementation
}

Prototype Scope

Unlike singleton scope, prototype scope creates a new instance of the bean every time it is requested from the container. Therefore, every call to getBean() for a bean with prototype scope will create a new object instance. This is particularly useful when you want to maintain stateful or stateless objects and avoid shared instances.

To specify prototype scope for a bean in Spring, use the @Scope("prototype") annotation. For example:

@Component
@Scope("prototype")
public class MyPrototypeBean {
    // class implementation
}

Request Scope

In a web application context, beans can be scoped to match the lifespan of an HTTP request. Request-scoped beans are unique to each individual HTTP request and are not shared across multiple concurrent requests.

To define a bean with request scope, use the @Scope("request") annotation. For example:

@Component
@Scope("request")
public class MyRequestBean {
    // class implementation
}

Session Scope

Similar to request scope, session scope is specific to HTTP sessions in a web application context. Each user session will have its own set of session-scoped beans, ensuring that the data is isolated at the user-session level.

To create a bean with session scope, use the @Scope("session") annotation. For example:

@Component
@Scope("session")
public class MySessionBean {
    // class implementation
}

Global Session Scope

The global session scope is applicable only when working with Portlet-based web applications. The lifespan of global session-scoped beans matches the lifespan of the Portlet session.

To declare a bean with global session scope, use the @Scope("globalSession") annotation. For example:

@Component
@Scope("globalSession")
public class MyGlobalSessionBean {
    // class implementation
}

Custom Scope

Apart from the predefined bean scopes provided by Spring, you can also define your own custom bean scope. Custom scopes come in handy when you have specific requirements that are not covered by the built-in scopes.

To create a custom scope, implement the org.springframework.beans.factory.config.Scope interface and register the custom scope in your application context.

public class MyCustomScope implements Scope {
    // implementation of the custom scope methods
}

// Register the custom scope in your application context
@Component
public class AppConfig implements WebMvcConfigurer {
    @Override
    public void addScopeHandlers(ScopeRegistry registry) {
        registry.registerScope("custom", new MyCustomScope());
    }
}

Conclusion

Understanding and configuring bean scope in the Spring Framework is crucial for managing the lifecycle and state of your beans. By selecting the appropriate scope for each bean, you can optimize memory usage, control shared dependencies, and ensure data isolation. With the flexibility to use predefined scopes or create custom scopes, the Spring Framework empowers developers to build robust and scalable applications with ease. So choose the right bean scope and maximize the potential of your Spring-powered applications!


noob to master © copyleft