Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Spring session scoped bean saved in HttpSession?

Since I don't have in depth knowledge of spring session scope implementation. Can anyone please tell me if it is wise to use Spring Session scoped beans, where HttpSession object is very crucial. Like a web application where thousands of users access the site simultaneously.

Is spring session scoped bean saved in HttpSession object?

Or even if HttpSession object only refers to the spring session scoped bean, are we not making session object heavy?

How is it different form storing any bean directly in HttpSession object (making HttpSession object heavy point of view)?

like image 755
Aniruddha Jagtap Avatar asked Feb 12 '12 20:02

Aniruddha Jagtap


People also ask

How does a spring session scope work?

The request scope creates a bean instance for a single HTTP request, while the session scope creates a bean instance for an HTTP Session. The application scope creates the bean instance for the lifecycle of a ServletContext, and the websocket scope creates it for a particular WebSocket session.

What is session scope bean?

Scopes a single bean definition to the lifecycle of a HTTP Session . Only valid in the context of a web-aware Spring ApplicationContext . Scopes a single bean definition to the lifecycle of a global HTTP Session . Typically only valid when used in a portlet context.

Can we use HttpSession in spring boot?

You can use Spring Session with HttpSession by adding a servlet filter before anything that uses the HttpSession . You can choose to do in any of the following ways: Java-based Configuration. XML-based Configuration.

What is spring boot HttpSession?

The HttpSession class type lets us know which implementation (e.g. Servlet Container vs. Spring Session) is being used to manage the HTTP Session state. The HTTP Request count is simply incremented every time a client HTTP Request is made to the HTTP server (e.g. Servlet Container) before the HTTP Session expires.


2 Answers

The object is not really stored in HTTP session. It is linked with session id and actually stored inside of the Spring context. A session listener is used to clean instances once session is closed. See SessionScope JavaDoc.

like image 129
Eugene Kuleshov Avatar answered Oct 20 '22 23:10

Eugene Kuleshov


Here's what the Spring docs say:

Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

"Heavy"? No heavier than the object you're putting in it. Sessions should have a finite lifetime. A few kbytes per session won't be the end of your application. A simple calculation for the number of simultaneous sessions and object memory requirements should reassure you about your app server memory settings. You can always increase min and max memory if needed.

Storing things in HTTP session happens the same whether you're a Spring bean or not. The bean factory just does some extra things to help manage the object lifecycle for you.

like image 45
duffymo Avatar answered Oct 20 '22 23:10

duffymo