struts+spring action应配置为scope="prototype"
2013-04-29 20:32
330 查看
<bean id="personAction" scope="prototype" class="quickstart.action.PersonAction"> <constructor-arg ref="personService" /> </bean>
在配置文件中,bean默认是单例模式,应用服务器启动后就会立即创建bean,以后就可以重复使用。
这带来一个问题,bean的全局变量被赋值以后,在下一次使用时会把值带过去。也就是说,bean是有状态的。
在web状态下,请求是多线程的,全局变量可能会被不同的线程修改,尤其在并发时会带来意想不到的bug。
而在开发时,访问量很小,不存在并发、多线程的问题,程序员极有可能会忽视这个问题。
所以在配置action bean时,应使用scope="prototype",为每一次request创建一个新的action实例。这 符合struts2的要求,struts2为每一个request创建一个新的action实例。当request结束,bean就会被jvm 销毁,作为垃圾收回。
当然,也可以设置scope="session",也能避免web中action的并发问题,只为当前用户创建一次bean,直至 session消失。在这种情况下,对当前用户而言,bean是有状态的。好处就是少创建bean的实例,有那么一 点点性能的提升
相关文章推荐
- struts2.0中spring配置文件中action的属性 scope="prototype"
- struts+spring action应配置为scope="prototype"
- struts+spring action应配置为scope="prototype"
- struts+spring action应配置为scope="prototype"
- struts2.0中spring配置文件中action的属性 scope="prototype" .
- struts+spring action应配置为scope="prototype"
- struts+spring action应配置为scope="prototype"
- struts+spring action应配置为scope="prototype"
- struts+spring action应配置为scope="prototype"
- struts+spring action应配置为scope="prototype"
- struts+spring action应配置为scope="prototype"
- struts2.0中spring配置文件中action的属性 scope="prototype"
- Struts2+hibernate+spring 配置文件中scope="prototype"的作用
- (上节)Spring+JDBC+Struts;实现图片上传;配置的属性默认dbc用单例模式,action一定要scope=“prototype”
- spring管理Struts2的action时应配置scope="prototype"
- spring action scope="prototype"
- Spring注解@Scope("prototype") Spring在Action上面注解@Scope("prototype")
- sping+struts2中配置Action的bean中scope="prototype"的作用
- Spring配置<bean>时加scope="prototype"的原因
- struts单例模式,和spring代理模式与非单例模式@Scope("prototype")