简体   繁体   中英

Spring “session” scope of a bean

It seems to me that "session" scope is another means to keep objects in session as

using setAttrubute / getAttribute

Correct?

You know, dont know why, it does not work for me.

<bean id="sabreUser" class="util.MyUser" factory-method="getSomeUser" scope="session">

<const args...>

What I see is that after the initialization and initial deploy the MyUser properties are correct.

Then, in the first session I change MyUser property, the session is closed.

The second session runs and it sees the last set value from the previous session!

What does that mean?

I expect this object to be initialized whenever a new session starts. But it better looks as singleton, though you see - "session" attribute is set.

I can see the cause of the problem in that a Servlet's fields is initialized with @Autowired so, once it is initialized, every other session will see its fields set and does not "ReWire" this properties. It happens once? How to overcome this problem?

Session-scoped beans are beans that live throughout the http session. They are stored in the session via setAttribute in some way.

So - yes.

The Spring session does not exactly match the HttpSession, and even the Spring documentation on the @SessionAttributes annotation says that it might be stored in the session or "some conversational storage". I got that from The Spring docs for 2.5 I've basically quit trying to make sense of it, and just got on with my life, if I want something stored in the HttpSession, I just have Spring inject the HttpSession to me, assuming you're using Spring MVC its pretty easy, instructions on the same page.

Session scoped beans are stored in Http Session by Spring framework. This scope is valid only in the context of Web application.It also works for Portlet envionments . When using in Portlet environment, there are two notions of session, application scope and portlet scope (default).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM