简体   繁体   English

Bean的春季“会话”范围

[英]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 使用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"> <bean id =“ sabreUser” class =“ util.MyUser” factory-method =“ getSomeUser” scope =“ session”>

<const args...> <const args ...>

What I see is that after the initialization and initial deploy the MyUser properties are correct. 我看到的是,在初始化和初始部署之后,MyUser属性是正确的。

Then, in the first session I change MyUser property, the session is closed. 然后,在第一个会话中,我更改MyUser属性,该会话关闭。

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. 我可以看到问题的原因在于Servlet的字段是通过@Autowired初始化的,因此,一旦初始化,每个其他会话都将看到其字段设置,并且不会“重新连接”此属性。 It happens once? 它发生一次吗? How to overcome this problem? 如何克服这个问题?

Session-scoped beans are beans that live throughout the http session. 会话范围的bean是在整个http会话中都存在的bean。 They are stored in the session via setAttribute in some way. 它们以某种方式通过setAttribute存储在会话中。

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". Spring会话与HttpSession并不完全匹配,甚至@SessionAttributes批注上的Spring文档都说它可能存储在会话中或“某些会话存储”中。 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. 我从Spring文档中获得了2.5版本,我基本上已经不再尝试去理解它了,并且继续我的生活,如果我想要存储在HttpSession中的东西,我只是让Spring注入HttpSession给我,假设你在同一页面上,使用Spring MVC非常简单。

Session scoped beans are stored in Http Session by Spring framework. 会话范围的bean通过Spring框架存储在Http Session中。 This scope is valid only in the context of Web application.It also works for Portlet envionments . 此作用域仅在Web应用程序上下文中有效。它也适用于Portlet封装。 When using in Portlet environment, there are two notions of session, application scope and portlet scope (default). 在Portlet环境中使用时,有两种会话概念,即应用程序范围和Portlet范围(默认)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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