Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What are the request and session scopes, and what happens if a request-scoped bean is used outside a web request?

Request scope gives one bean instance per HTTP request, and session scope gives one per HTTP session; there is also application scope, one instance per ServletContext. These scopes exist only in a web-aware application context, and they depend on a request bound to the current thread, so using a request-scoped bean outside a request, for example in a scheduled job or an @Async thread, fails with an IllegalStateException saying no thread-bound request was found.

Why this question is asked

Web applications often need data that belongs to one request, such as the current user or a correlation id, or to one user's session, such as a wizard's progress. Spring's web scopes let us put that data in beans and inject them like any other. Interviewers ask about them because the scopes have a sharp edge: they only work while a request is being handled on the current thread, and code that runs elsewhere breaks in a way that confuses many developers. In this answer, we will recap the web scopes, see how Spring knows which request is current, reproduce the failure outside a request, and look at application scope.

1A Quick Recap of the Web Scopes

A scope decides how many instances of a bean exist and how long each lives. Besides singleton and prototype, Spring has scopes tied to the servlet model. Request scope creates one instance per HTTP request, and the instance is discarded when the request ends. Session scope creates one instance per HTTP session, the server-side state a servlet container keeps for a user across requests, identified by a cookie such as JSESSIONID. Application scope creates one instance per ServletContext, the object representing the whole web application in the servlet container.

We declare them with @RequestScope, @SessionScope and @ApplicationScope, or with @Scope("request") and friends. The annotation forms also create a scoped proxy by default, which lets singletons inject them; we will meet that again below.

Java
@Component
@RequestScope
class RequestInfo {
private String correlationId;
void setCorrelationId(String id) { correlationId = id; }
String correlationId() { return correlationId; }
}
@Component
@SessionScope
class WizardState implements Serializable {
private int step = 1;
void next() { step++; }
int step() { return step; }
}
// Request 1, session A: RequestInfo #1, WizardState #A (step 1 -> 2)
// Request 2, session A: RequestInfo #2, WizardState #A (step 2 -> 3)
// Request 3, session B: RequestInfo #3, WizardState #B (step 1)
Web scopes at a glance
ScopeOne instance perLives until
requestHTTP requestthe request completes
sessionHTTP sessionthe session expires or is invalidated
applicationServletContextthe web application stops
websocketWebSocket sessionthe WebSocket session closes
All of them need a web-aware application context.

Notice that each request gets a fresh RequestInfo, while the WizardState follows the user's session across requests.

2How Spring Knows Which Request Is Current

These scopes are only registered in a web-aware application context, such as the one Spring Boot creates for a servlet web application. In a plain context, using @RequestScope fails because no scope named request exists.

Inside a web application, Spring needs to find the current request whenever a request-scoped bean is looked up. It does this with RequestContextHolder, a class that keeps the current request's attributes in a ThreadLocal, a variable with a separate value for each thread. The DispatcherServlet, or the RequestContextFilter that Spring Boot registers, binds the request to the thread when the request starts and clears it when it ends. The request scope then stores its bean instances as attributes of that request, and the session scope stores them as attributes of the HttpSession.

So the lookup is really: take the thread's bound request, look for an attribute with the bean's name, and create the bean if it is not there. That design is fast and simple, but it ties the scope to the thread that is handling the request.

3What Happens Outside a Web Request

Now consider code that is not running on a request thread: a @Scheduled job, a Kafka listener, a CommandLineRunner at startup, or an @Async method that runs on a pool thread. RequestContextHolder has nothing bound for that thread, so when such code calls a method on the request-scoped bean, the lookup fails.

Java
@Component
class NightlyReport {
private final RequestInfo info; // scoped proxy, injected fine at startup
NightlyReport(RequestInfo info) { this.info = info; }
@Scheduled(cron = "0 0 2 * * *")
void run() {
log.info("cid={}", info.correlationId());
// ScopeNotActiveException: Error creating bean with name 'scopedTarget.requestInfo':
// Scope 'request' is not active for the current thread ...
// Caused by: java.lang.IllegalStateException: No thread-bound request found:
// Are you referring to request attributes outside of an actual web request ...
}
}

Notice that injection itself worked, because the singleton received a proxy, not the real bean. The failure happens at the first method call, when the proxy tries to find the current request. The underlying cause is always the IllegalStateException about no thread-bound request; recent Spring versions wrap it in ScopeNotActiveException, a kind of BeanCreationException (the exact wording varies by version).

The fix is not to make the scope work everywhere, but to avoid using it where there is no request. Background code should receive the values it needs as parameters, or read them from a context object that works for its own entry point.

4Application Scope Versus Singleton

Application scope looks like singleton, since both give one instance for the application, but there are two differences. An application-scoped bean is stored as an attribute of the ServletContext, so other code in the same web application that is not Spring-managed, such as a plain servlet or a JSP, can see it. And a ServletContext can be shared by several Spring application contexts, for example a root context and a DispatcherServlet context, so the instance is per ServletContext rather than per Spring context. In a typical Spring Boot application with one context, the practical difference is small, and singleton is the usual choice.

5Follow-up questions

Can a session-scoped bean be used from a background thread if we pass the session along?

Not through the scope, because the scope looks up the thread-bound request, not a session we hold. We could read and write the session attributes directly from the HttpSession object, but the session might expire or be serialised in the meantime. It is safer to copy the values the background work needs into an object passed to it.

Why does injecting a request-scoped bean into a singleton work at all?

Because @RequestScope creates a scoped proxy by default. At startup the singleton receives the proxy, which exists without any request. Every method call on the proxy looks up the real instance for the current request. Without a proxy, creating the singleton would require a real request-scoped instance at startup, and that fails because no request is active.

Is request scope available in WebFlux applications?

No. Request and session scopes depend on the servlet request bound to a thread, and WebFlux does not bind requests to threads. In reactive applications, per-request data travels in the exchange attributes or in the Reactor context, and session data uses WebSession.

6Conclusion

In this answer, we saw that request scope gives one instance per HTTP request, session scope one per HTTP session, and application scope one per ServletContext, all available only in a web-aware context. Spring finds the current request through RequestContextHolder, which binds it to the handling thread, so using a request-scoped bean from a scheduled job, a listener or an @Async thread fails with an IllegalStateException about no thread-bound request, usually wrapped in a ScopeNotActiveException.

What to say in the interviewAbout 80 seconds aloud

Open with

Request scope means one bean instance per HTTP request, created on first use in that request and discarded when the request ends. Session scope means one instance per HTTP session, so it follows a user across requests, and the instance is stored as a session attribute.

Then, if they want more

There is also application scope, one instance per ServletContext, stored as a ServletContext attribute, which is close to a singleton but visible to non-Spring web code and shared across Spring contexts in that servlet context. These scopes only exist in a web-aware application context.

To find the current request, Spring uses RequestContextHolder, which holds the request in a ThreadLocal; the DispatcherServlet or RequestContextFilter binds it at the start of the request and clears it at the end.

So if code that is not running on the request thread uses a request-scoped bean, like a @Scheduled job, a Kafka listener or an @Async method, there is no bound request and the call fails with an IllegalStateException, "No thread-bound request found", which newer Spring versions wrap in a ScopeNotActiveException.

Injection into singletons still works because @RequestScope creates a proxy by default; the failure appears at the first method call. The fix is to pass the needed values explicitly to background code.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What are the request and session scopes, and what happens if a request-scoped bean is...": https://scrivio-interview-library.netlify.app/spring-boot/bean-scopes/fabf435066/