How can I get session from a common class in Spring Boot?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If a “common class” in Spring Boot needs access to session data, the best solution is usually not to fetch the session statically. Instead, inject the request or session through Spring-managed beans, or better yet, pass the specific values the class actually needs. That keeps the code testable and avoids turning session access into hidden global state.
The Straightforward Option: Inject HttpSession
If the class is Spring-managed and used within an HTTP request, you can inject HttpSession directly.
This works because Spring can proxy request-bound objects into normal beans when the context is correct.
Prefer Passing the Needed Value When Possible
Even though session injection works, many classes do not really need the entire session object. They need one value from it.
A cleaner controller-to-service flow is often:
That design is easier to test because AuditService is no longer tied to the web layer.
Avoid Static Session Access Patterns
A common anti-pattern is trying to grab the session from a static utility or from code that Spring does not manage. That usually leads to fragile workarounds, hidden dependencies, and code that breaks outside web requests.
If the class is not a Spring bean, fix that first. Spring can only inject request and session state into objects it controls.
If You Really Need Request Context Access
As a fallback, Spring exposes request attributes through RequestContextHolder, but it should be treated as an escape hatch rather than a first choice.
This works only inside an active request thread and is harder to mock than dependency injection.
Know When Session Access Is the Wrong Abstraction
If many common classes keep reaching into session state, the design often needs a dedicated user-context abstraction instead. That gives the application one place to interpret session-backed identity, locale, tenant, or preferences.
Then the rest of the code depends on CurrentUserContext or a similar interface, not on servlet APIs directly.
Keep Business Logic Session-Agnostic
The more your core services depend directly on servlet session APIs, the harder they become to test outside HTTP requests. A good boundary is to extract the user id, tenant id, or locale once near the web layer and pass those values downward.
That keeps the session as an input source rather than as a hidden dependency spread across the codebase.
Common Pitfalls
The biggest mistake is trying to access session state from a static helper or a non-Spring-managed object.
Another issue is injecting HttpSession everywhere when most code only needs one or two already-resolved values.
A third problem is using RequestContextHolder casually in deep business logic, which makes the code harder to test and more tightly coupled to the web layer.
Summary
- If the class is Spring-managed, injecting
HttpSessioncan work. - Passing the specific session-derived value is often cleaner than passing the whole session.
- Avoid static session access patterns and non-managed utility code.
- Use
RequestContextHolderonly as a fallback, not as the default design. - If session access spreads widely, introduce a dedicated user-context abstraction.
Related reading
- How can I get System variable value in Java?
- How can I get the current date and time in UTC or GMT in Java?
- How can I get the current stack trace in Java?
- How can I get the latest JRE / JDK as a zip file rather than EXE or MSI installer?
- How can I get the SQL of a PreparedStatement?
- How can I hash a password in Java?
- How can I implement a Runnable with timeout?
- How can I implement static methods on an interface?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.