spring缓存问题

来源:互联网 发布:cnc程序模拟软件 编辑:程序博客网 时间:2024/06/05 10:32

今天在使用分页进行查询的时候,出现一个问题:每次我选中特定的条件进行查询,当我重新点击struts的action时,按道理,是应该重新回到最初的没有选择任何查询条件的页面,但不管我怎么点击,重新发起请求,每次的结果都是按刚开始的查询条件查询出来的结果。

因为使用的Spring + mybatis + struts的框架,经同事指点,发现spring配置文件的,相应的action配置如下:


<bean id="AlarmInfoAction" class="com.cstor.network.alarm.action.AlarmInfoAction" >
<property name="alarmInfoService" ref="alarmInfoServiceImpl"/>
</bean>


添加上scope范围之后,问题圆满解决:

<bean id="AlarmInfoAction" class="com.cstor.network.alarm.action.AlarmInfoAction"scope="prototype">
<property name="alarmInfoService" ref="alarmInfoServiceImpl"/>
 </bean>


对于spring的prototype范围,Spring官方文档对prototype的解释如下:

The prototype scope

The non-singleton, prototype scope of bean deployment results in thecreation of a new bean instance every time a request for that specific bean is made. That is, the bean is injected into another bean or you request it through a getBean()method call on the container. As a rule, use the prototype scope for all stateful beans and the singleton scope for stateless beans.The following diagram illustrates the Spring prototype scope.A data access object (DAO) is not typically configured as a prototype, because a typical DAO does not hold any conversational state; it was just easier for this author to reuse the core of the singleton diagram.



The following example defines a bean as a prototype in XML:

<!-- using spring-beans-2.0.dtd -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>


In contrast to the other scopes, Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, and otherwise assembles a prototype object, and hands it to the client, with no further record of that prototype instance. Thus, althoughinitialization lifecycle callback methods are called on all objects regardless of scope, in the case of prototypes, configureddestructionlifecycle
callbacks arenotcalled. The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding. To get the Spring container to release resources held by prototype-scoped beans, try using a custombeanpost-processor, which holds a reference to beans that
need to be cleaned up. In some respects, the Spring container's role in regard to a prototype-scoped bean is a replacement for the Javanewoperator. All lifecycle management past that point must be handled by the client. (For details on

the lifecycle of a bean in the Spring container, see the section called “Lifecycle callbacks”.)


  scope="prototype"(多态)是在每次用户发起请求时重新生成action对象,对于多线程访问不会出现问题,如果没有配置scope=prototype则添加的时候不会新建一个action,他任然会保留上次访问的过记录的信息

      默认是scope="singleton"(单态),这些bean被spring初始化后,始终只有一份,很适用于无状态的bean,DAO、Service都采用的这种。当然,scope的值还有session,request等等。


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 不灭战神 我在宝可梦世界开餐厅 爸爸,我是来当你同学的 这个主角明明很强却异常谨慎 四合院:我的穿越为啥这么陋 甩了线上男友后我被亲哭了 人在南天坐看万古 为了成为英灵我只好在历史里搞事 我成了游戏里的反派之王 离婚后,前夫每天都想上位 霍格沃茨的风与鹰翼 攻略暴君后,我抱错大腿了 我的精灵模拟器 金鸾喜嫁 人在美漫,开局枪斗术 大国科技从手机开始 人住超神,渣在诸天 斗罗之王者吕布降临 从白蛇开始诸天改命 诸天之天外降临者 屠龙之前就读过龙族的路明非 足球盛宴,从收购欧洲俱乐部开始 在漫威驱魔的魔鬼神父 柯南之我的老姐毛利兰 重生在电影的世界 美漫第一仙 签到从遮天开始 风云饲养师 穿越异世:开局拥有空间神器 吞噬星空之超脱之路 穿梭两界:我携带的物品能变强 从开端开始的超凡之旅 相亲后,靳先生突然黏上我 港综世界的炼金术师 惊!帝君娇宠的小废柴竟然有空间 海上升明帝 开局十连抽,超跑豪宅拿来吧你! 柯学世界的普通警察 柯学世界里的柯研人 我只是个平平无奇的道士啊 梦回之苟在深圳做房东