Quick revision
The short answer
My guidance would be that shared libraries must never depend on web scopes or RequestContextHolder, because the same code will run from Kafka listeners, batch jobs and async tasks where no request exists. Context such as user, tenant or trace id should be passed explicitly or through a small framework-neutral context object that each entry point fills, web scopes should stay in the web layer, the rules for propagating context across threads and messages should be written down, and existing libraries should be reviewed for RequestContextHolder use.
What to remember
In this answer, we saw that request and session scopes assume a thread-bound HTTP request, an assumption that breaks when shared code is called from messaging, batch and async paths, sometimes silently. The guidance is to keep web scopes and RequestContextHolder out of shared libraries, pass context explicitly or through a framework-neutral context object set at every entry point, keep web scopes in the web layer, document propagation across threads and messages, and back it with build rules, shared adapters and tests from both web and non-web entry points.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
My guidance would be that shared libraries must never depend on web scopes or RequestContextHolder, because the same code will run from Kafka listeners, batch jobs and async tasks where no request exists. Context such as user, tenant or trace id should be passed explicitly or through a small framework-neutral context object that each entry point fills, web scopes should stay in the web layer, the rules for propagating context across threads and messages should be written down, and existing libraries should be reviewed for RequestContextHolder use.
Why this question is asked
Web scopes were designed for a world where every unit of work was an HTTP request handled on one thread. Modern systems also consume messages, run batch jobs, call services asynchronously and use reactive pipelines. Shared code written for the old world fails in surprising ways in the new one, often only in production and only on some code paths. Interviewers ask this leadership question to see whether we can set architectural guidance that prevents a whole class of bugs, and explain it so teams follow it. In this answer, we will see why web scopes leak into shared code, define the guidance, show the pattern that replaces them, and look at how to roll it out.
1Why Web Scopes Leak Into Shared Code
A request-scoped bean is convenient. A library that needs the current user can inject a request-scoped CurrentUser proxy and call it from anywhere, without the caller passing anything. RequestContextHolder offers the same convenience statically: any code can ask for the current HttpServletRequest. Over years, such calls spread into audit helpers, data access filters, logging utilities and client libraries.
The hidden assumption is that the code always runs on a thread that is handling an HTTP request. When a team reuses the audit helper from a Kafka listener, a nightly batch job or an @Async method, the assumption breaks. The call fails with an IllegalStateException about no thread-bound request, or, worse, a defensive library catches the error and records the action as done by nobody or by a default tenant. The second case is silent and can be a security or data isolation problem.
2The Guidance
The first rule is that code in shared libraries must not depend on web scopes, RequestContextHolder or servlet types. A shared library cannot know which entry points will call it, so it must assume any of them. The second rule is that context, such as the current user, tenant, locale and trace id, is either passed explicitly as method parameters or read from a framework-neutral context object, a small type owned by the platform that holds the values and has no knowledge of HTTP. The third rule is that web scopes, if used at all, stay in the web layer: controllers, filters and web-specific adapters.
The fourth rule is to document how context propagates. Each entry point type, HTTP, Kafka, scheduled jobs and async executors, has a documented place where context is set and cleared, and each thread or process boundary has a documented way to carry it: a TaskDecorator or context-propagation for executors, message headers for Kafka, explicit fields for batch job parameters. The fifth rule is to review existing shared libraries for RequestContextHolder and web scope use, and to fail fast with a clear message when context is missing, rather than defaulting.
- Entry pointsHTTP filter, Kafka interceptor, job launcher, async decorator
- Edge adaptersread headers or job parameters; set and clear context
- Framework-neutral context
RequestContextwith user, tenant, trace id - Shared librariestake context as parameters or from the neutral object
- Infrastructuredatabase, messaging, HTTP clients
5 layers, from the top down. Step through them one at a time.
Only the top two layers know about HTTP or Kafka.
3What the Replacement Looks Like
The platform team provides one small context type and one adapter per entry point. Shared code depends only on the context type.
Notice that the shared audit service now works identically from HTTP, Kafka and batch, and that a missing context produces a clear error that says where to fix it. For important business values, explicit method parameters are still preferred, because they cannot be lost at a thread boundary; the context object is for cross-cutting values used in many places.
4Rolling It Out
Guidance alone changes little, so we combine it with tooling. An ArchUnit rule in the shared build forbids library packages from importing RequestContextHolder, jakarta.servlet types or web scope annotations, with an allow-list for existing violations that shrinks over time. The platform starter ships the context type and the adapters for each entry point, so teams do not write their own. Each shared library gets tests that call it from a web entry point and from a non-web entry point, which is the test that would have caught the original problem.
We then review the most used shared libraries first, since one fix there helps many services, and track the number of remaining violations. Explaining the reason with a real incident, such as an audit record with no user from a Kafka path, helps teams see why the rule is worth following.
5Follow-up questions
Is RequestContextHolder ever acceptable?
Yes, inside web-specific code that only runs during request handling, such as a filter, an argument resolver or a controller helper. The problem is only when it is used in code that may be called from other entry points. Even in web code, an explicit method argument or a @RequestHeader parameter is usually clearer.
Why not just copy request attributes to other threads?
Because messaging and batch code never had a request to copy, and copying a finished request to an async thread is unsafe since the container may recycle it. A neutral context object with plain values works for all entry points and can be carried across threads and in message headers.
How does this relate to Spring Security's SecurityContextHolder?
SecurityContextHolder is also thread-bound, but it is not tied to HTTP: a Kafka adapter can set an Authentication for the listener's work and clear it afterwards. That makes it usable as part of the neutral context for the user identity, as long as each entry point sets and clears it and each thread boundary propagates it, for example with Spring Security's delegating executors.
6Conclusion
In this answer, we saw that request and session scopes assume a thread-bound HTTP request, an assumption that breaks when shared code is called from messaging, batch and async paths, sometimes silently. The guidance is to keep web scopes and RequestContextHolder out of shared libraries, pass context explicitly or through a framework-neutral context object set at every entry point, keep web scopes in the web layer, document propagation across threads and messages, and back it with build rules, shared adapters and tests from both web and non-web entry points.
Open with
My guidance would start from the fact that shared code cannot know who calls it. A library that injects a request-scoped bean or calls RequestContextHolder assumes it runs on an HTTP request thread, and that breaks as soon as a Kafka listener, a batch job or an @Async method calls it.
Then, if they want more
Sometimes it fails loudly with no thread-bound request, and sometimes a defensive library silently uses a default user or tenant, which is worse. So the rules are: no web scopes, RequestContextHolder or servlet types in shared libraries.
Context like user, tenant and trace id is passed explicitly, or read from a small framework-neutral context object that each entry point sets and clears, an HTTP filter, a Kafka interceptor, a job launcher. Web scopes stay in the web layer. We document how context crosses boundaries: a task decorator for executors, headers for messages, parameters for batch jobs.
And code fails fast with a clear message when context is missing.
To make it stick, I would put the context type and adapters in the platform starter, add an ArchUnit rule banning those imports in library packages with a shrinking allow-list, review the most used libraries first, and require tests that call each library from both web and non-web entry points.
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.
- Official documentation
- Spring Framework reference, Core technologies
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 "Request and session scopes tie application code to the servlet request model. As your...": https://scrivio-interview-library.netlify.app/spring-boot/bean-scopes/51a2150dc9/