1. Introduction

In this tutorial, we'll discuss what are the differences between request scope and session scope in Spring. If you need to know more about Spring bean scopes, check out this tutorial.

First, let's explain when do we use these scopes and how do we configure our beans to use them in the Spring application.

2. Request scope

This scope is used in web-aware Spring Application Context. The idea of this bean scope is to have one instance of the bean per HTTP request.

<bean id = "beanId" class = "beanClass" scope = "request">
..other bean configuration...
</bean>

In case we're using annotation-based configuration, we can do something like this:

@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public MyBean myBeanRequest() {
    return new MyBean();
}

Notice here that we're using the proxyMode parameter that is necessary when we define request scope with the @Scope parameter. It's because there is no active request when the Application context is initialized. This way, Spring creates a proxy object to be injected as a dependency, and our bean will be instantiated when the request happens.

There's also a shorter version for this configuration in the shape of the @RequestScope annotation.

@Bean
@RequestScope
public MyBean myBeanRequest() {
    return new MyBean();
}

3. Session scope

This is also a scope that's used in the web-aware Application Context. It's created per every HTTP session and is present during the whole HTTP session lifecycle.

To define session scope through XML configuration file, we should do something like this:

<bean id = "beanId" class = "beanClass" scope = "session">
..other bean configuration...
</bean>

Notice that, as in the request scope, we need proxyMode parameter - this will create a proxy object that will be used for dependency injection until the session is opened and instance of the session-scoped bean created. Now, let's see how to define bean with session scope:

@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public MyBean myBeanSession() {
    return new MyBean();
}

As with request scope, we have a shorter way to define this scope through annotation by using @SessionScope:

@Bean
@SessionScope
public MyBean myBeanSession() {
    return new MyBean();
}

4. Difference Between Request Scope and Session Scope

First, let's mention that the HTTP protocol is stateless. If we set the bean scope to request and a user makes more than one request for a web page in his/her user session, then a new bean would be created on every request.

In case that bean scope is defined as session, if a user makes a request for a web page more than once, then the same bean is used on every request as long as the requests are within the same user session and made from a client which is capable of maintaining the session. For example, if we're using curl, we can't expect it to maintain the user session unless we pass the cookie/session identifier header.

5.  Conclusion

In this tutorial we explained Spring request and session scopes and also pointed out to their differences. For more information on Spring bean scopes, check out the official Spring documentation.