Is Spring session scoped bean saved in HttpSession?

烂漫一生 提交于 2019-12-06 03:45:34

问题


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)?


回答1:


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.




回答2:


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.




回答3:


Is spring session scoped bean saved in HttpSession object?

If the HttpSession object you mentioned here is actually provided by spring-session (spring-session wraps the httpSession object in original httpRequest), the answer would be YES -> indeed, all the spring session scoped beans are saved in the httpSession provided by spring-session as attributes.

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

No. The "heaviness" of the httpSession very much depends on the objects you put in. In addtion, session beans are meant to be ephemeral. Last but not least, the session object in micro-services world is usually offload to a stand-alone store, like Redis or HazelCast. So, it won't be considered as "heavy" in terms of memory consumption.

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

No difference at all (assuming you are using spring-sesion), as all the all the spring session scoped beans are saved in the httpSession provided by spring-session as attributes.



来源:https://stackoverflow.com/questions/9252586/is-spring-session-scoped-bean-saved-in-httpsession

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!