关于Scope 'session' is not active for the current thread的问题
2014-04-23 23:18
495 查看
最近在做毕业设计,已近完成,却发现重大bug。
【问题概述】:我做的是订餐系统,测试时发现不用A客户可以看到B客户购物车中的餐品。由于我以前做过的类似的系统是没有用spring整合的,购物车这里可以与session绑定解决这个问题,如今使用spring整合却忽略了这个问题(好久没写代码了),如下本文就该问题给出解决方案,方案均来自互联网并非本人自己想到的,遂记下学习之。
【解决方法】:
造成这个问题的原因是spring默认所有的bean都为单例模式(scope="singleton"),固A,B客户操作的其实是同一个购物车实例。想要解决这个问题就需要将生命周期设置为session,即将Scope设置为scope="session",表示为一个session只创建一个实例。
将Scope设置为scope="session"需要在web.xml中做如下配置:
<!-- 开启Session机制 -->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
此外还需要在spring配置文件中.
相应的spring bean配置中加入<aop:scoped-proxy/>
<bean id="cartService" class="com.lvsl.bysj.service.impl.CartServiceImpl" scope="session">
<aop:scoped-proxy/>
<property name="cartDao" ref="cartDao"></property>
</bean>
【问题概述】:我做的是订餐系统,测试时发现不用A客户可以看到B客户购物车中的餐品。由于我以前做过的类似的系统是没有用spring整合的,购物车这里可以与session绑定解决这个问题,如今使用spring整合却忽略了这个问题(好久没写代码了),如下本文就该问题给出解决方案,方案均来自互联网并非本人自己想到的,遂记下学习之。
【解决方法】:
造成这个问题的原因是spring默认所有的bean都为单例模式(scope="singleton"),固A,B客户操作的其实是同一个购物车实例。想要解决这个问题就需要将生命周期设置为session,即将Scope设置为scope="session",表示为一个session只创建一个实例。
将Scope设置为scope="session"需要在web.xml中做如下配置:
<!-- 开启Session机制 -->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
此外还需要在spring配置文件中.
相应的spring bean配置中加入<aop:scoped-proxy/>
<bean id="cartService" class="com.lvsl.bysj.service.impl.CartServiceImpl" scope="session">
<aop:scoped-proxy/>
<property name="cartDao" ref="cartDao"></property>
</bean>
相关文章推荐
- Scope 'session' is not active for the current thread
- org.springframework.beans.factory.BeanCreationException: Scope 'request' is not active for the current thread
- 解决Scope 'request' is not active for the current thread异常
- getCurrentSession()抛出is not valid without active transaction和no session found for current thread异常分析
- Spring报错——Scope 'session' is not active for the current thread
- iOS学习笔记13-iOS 'The sandbox is not sync with the Podfile.lock'问题解决
- this class is not key value coding-compliant for the key buttonPressed.' 错误解决方式
- 关于myBatis的问题There is no getter for property named 'USER_NAME' in 'class com.bky.model.实例类'
- 关于编写WPF UserControl时提示The name 'InitializeComponent' does not exist in the current contextr的解决!
- 【转】关于Hibernate Could not obtain transaction-synchronized Session for current thread
- 错误整理:Unable to add module to the current project as it is not of packaging type 'pom'
- "this class is not key value coding-compliant for the key ..."问题的解决(转)
- egit 出现问题 The current branch is not configured for pull No value for key branch.master.merge found i
- 关于Hibernate Could not obtain transaction-synchronized Session for current thread
- 关于Spring3.2+Hibernate4.1整合问题:No Session found for current thread
- 关于异常“The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine”的处理
- The run destination My Mac 64-bit is not valid for Running the scheme '*****'.
- 关于两个jar包中存在包名和类名都完全相同的问题 (...is not applicable for the arguments...)
- 关于spring配置中the prefix "tx" for element "tx:annotation-driven" is not bound 问题的处理
- Trouble shooting:login failed for user 'sa' ,the user is not associated with a trusted SQL Server