spring中bean的scope属性理解
bean的scope属性有prototype,singleton,request, session几个属性
spring和struts2整合的时候,struts2的action要配置成scope="prototype",这是为了线程安全,
下面是struts2+hibernate+spring配置文件的一部分,以前都是仿造已经写好的bean的配置。有一次 scope="prototype"忘记写了结果出了问题 ,其默认是scope="singleton",唯一的。
项目中对一个表的增删该操作是用一个action,这个action有add,update,delete,save这些方法,添加和修改是共用一个页面,当页面得到id时代表进行的修改操作,反之是添加操作。因为在配置spring的bean是忘了写scope="prototype"所以每次添加时都显示最后一次访问过的记录。
找了很长时间,原来是spring bean出了问题。 scope="prototype" 会在该类型的对象被请求时创建一个新的action对象。如果没有配置scope=prototype则添加的时候不会新建一个action,他任然会保留上次访问的过记录的信息。
<bean id="assetAction" class="com.servicezone.itsd.asset.webapp.action.AssetAction" scope="prototype">
既然action配置了scope="prototype",那么action里的bean就不能配置成scope="request"和scope="session",不然会报错:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customer': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
这也可以理解,action是每次都是新的,但是里面的属性却不是
最新文章
- EntityFrame CodeFirst 自动生成表
- your PC ran into a problem and needs to restart system_thrread_exception_not_handled
- 转-Asp.Net MVC及Web API框架配置会碰到的几个问题及解决方案
- C#字符串的恒定性
- Textbox像百度一下实现下拉显示 z
- AS 学习笔记 加载数据
- ubuntu修改系统环境变量文件导致起不来
- json2.js参考
- 隐藏android中EditText中的下划线以及修改光标颜色
- HDU 1009 FatMouse&#39; Trade(贪心)
- Supervisor 安装及配置管理uwsgi进程
- .net连接数据库递归
- Windows下访问控制管理
- BZOJ2961 共点圆[CDQ分治]
- springboot学习笔记-6 springboot整合RabbitMQ
- In House打包流程
- 解决Oracle11g密码180天过期,账号锁住的问题
- Struts2 学习笔记 09 访问Web元素
- HDU 1710 Binary Tree Traversals(二叉树)
- Android Studio 2.3.3 调用asp.net webService实战(拒绝忽悠)
热门文章
- 为什么要使用“var me=this”这样的写法
- 【cocos 2d-x】VS2013+cocos2d-x3.3Final+Adriod交叉编译环境配置(超详细版)
- Linux网络设置(第二版) --Linux网络设置
- Jamon
- LeetCode(56)-Add Binary
- rails将类常量重构到数据库对应的表中之二
- ruby中如何直接编译运行C代码
- Oracle12c(12.1)中性能优化&;amp;功能增强之通过参数THREADED_EXECTION使用多线程模型
- Salesforce Lightning开发学习(一)Hello World开发实践
- js中用var与不用var的区别