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是每次都是新的,但是里面的属性却不是
spring中bean的scope属性理解的更多相关文章
- Spring中bean标签的属性和值:
Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...
- spring中bean的作用域属性singleton与prototype的区别
1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...
- spring中bean的常用属性
一.scop scope用来配置bean对象是否是单例模式.单例模式是java的二十三种设置模式之一,指在这个项目运行过程中一 个类的对象只会实例化一次.一般,工厂类的对象都是单例模式.非单例模式叫多 ...
- spring学习笔记(四)我对spring中bean生命周期的理解
我相信大部分同学对spring中bean的生命周期都不陌生,但是如果要详细的说出每一个步骤,可能能说出来的也不多,我之前也是这样,前几天调了一下spring的源码,看了一点书,突然一下明朗了,理解了s ...
- Spring中bean的scope
Spring容器中的bean具备不同的scope,最开始只有singleton和prototype,但是在2.0之后,又引入了三种类型:request.session和global session,不 ...
- Spring中bean的scope详解
如何使用spring的作用域: <bean id="role" class="spring.chapter2.maryGame.Role" scope=& ...
- Spring -10 -<bean>的 scope 属性 -singleton 默认值/prototype 多例 /request /session /application /global session
1.<bean>的属性; 2.作用:控制对象有效范围(单例,多例等)3.<bean/>标签对应的对象默认是单例的. 3.1无论获取多少次,都是同一个对象 Teacher t1 ...
- spring中bean的高级属性之list, set, map以及props元素(含举例)
转自:http://qingfeng825.iteye.com/blog/144704 list, set, map和props元素分别用来设置类型为List,Set,Map和Propertis的属性 ...
- spring中bean的作用域属性single与prototype的区别
https://blog.csdn.net/linwei_1029/article/details/18408363
随机推荐
- 03_Nginx添加新模块
1 进入nginx安装目录,查看nginx版本及其编译参数: [root@localhost nginx]# ./nginx -V nginx version: nginx/1.8.0 buil ...
- 【一天一道LeetCode】#7. Reverse Integer
一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...
- iOS下FMDB的多线程操作(二)
上一篇记录不使用FMDatabaseQueue来使用多线程,这一篇记录一下使用FMDatabaseQueue的方式. 需要注意的时queue操作中不能嵌套queue操作,否则会各种错误. 当使用FMD ...
- obj-c属性的新的特性
在以前的objc中我们必须在接口中定义属性对应的实例方法,然后在实现文件中"同步"该属性,如下代码: @interface Foo:NSObject{ NSString *name ...
- Hibernate之初体验
在开始学Hibernate之前,一直就有人说:Hibernate并不难,无非是对JDBC进一步封装.一句不难,难道是真的不难还是眼高手低? 如果只是停留在使用的层面上,我相信什么技术都不难,看看别人怎 ...
- fastDFS与Java整合上传下载
由于项目需要整合个文件管理,选择使用的是fastDFS. 整合使用还是很方便的. 准备 下载fastdfs-client-java源码 源码地址 密码:s3sw 修改pom.xml 第一个plugin ...
- 如何用Python网络爬虫爬取网易云音乐歌曲
今天小编带大家一起来利用Python爬取网易云音乐,分分钟将网站上的音乐down到本地. 跟着小编运行过代码的筒子们将网易云歌词抓取下来已经不再话下了,在抓取歌词的时候在函数中传入了歌手ID和歌曲名两 ...
- java main方法执行sql语句
public static void main(String[] args) throws Exception{ String driver = "oracle.jdbc.driver.Or ...
- 【个人学习笔记】走近H5
一.HTML5概述 1.HTML5新特性 兼容性(ie9+).合理性.效率.安全性.分离.简化.通用性.无插件 2.HTML5构成 主要包括下面这些功能:Canvas(2D和3D).Channel消息 ...
- Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...