为什么struts2的action是线程安全的,struts1的action不是线程安全的?
先对struts1和struts2的原理做一个简单的讲解
对于struts1 ,当第一次**.do的请求过来时,在内存中的actionmapping中找到相对应的action,然后new出这个action放在缓存中,当第二次一样的请求过来时,还是找的这个action,所以对于struts1来说,action是单实例的 ,只有一个,如果在action中定义变量,就要非常小心了,因为并发问题,可能带来灾难性的后果,也不是不可以,我们可以加锁达到同步,只是在性能上就要折衷了。
另外说几句 ,当struts交由spring管理的时候 ,spring的bean配置默认是单例的 ,
如果action是有状态的 ,必须显示的配置为prototype
<bean id="saveUserAction" class="com.test.action.user.SaveUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean>
下面是struts1.2的源码:
当请求过来时,去找指定的action,如果有就直接取出来,如果没有就new一个新的action放到map中。
/**
* The set of Action instances that have been created and
* initialized, keyed by the fully qualified Java class name of the
* Action class.
*/
protected HashMap actions = new HashMap();
processActionCreate这个方法里去一窥究竟吧:
1、先获取类名
2、根据类名去一个名为actions的map里查寻实例是否已经存在
3、如果存在,则直接返回
4、如果不存在,则创建一个新实例
5、把创建好的action放到map里备用
protected Action processActionCreate(HttpServletRequest request,
HttpServletResponse response,
ActionMapping mapping)
throws IOException {
// Acquire the Action instance we will be using (if there is one)
String className = mapping.getType();//1、先获取类名
...
Action instance = null;
synchronized (actions) {
// Return any existing Action instance of this class
instance = (Action) actions.get(className);//2、根据类名去map里查寻实例是否已经存在
if (instance != null) {
return (instance); //3、如果存在,则直接返回
}
// Create and return a new Action instance
//4、如果不存在,则创建一个新实例
instance = (Action) RequestUtils.applicationInstance(className)
instance.setServlet(this.servlet);
actions.put(className, instance);//5、把创建好的action放到map里
}
...
return (instance);
}
struts2 在struts1的基础上做了改进 ,对于struts2 ,每次请求过来都会new一个新的action , 所以说struts2的action是线程安全的 , 但同时也带来一个问题,每次都new一个action ,这样action的实例太多 , 在性能方面还是存在一定的缺陷的。
struts1是单例模式,而struts2是原型模式
分享到:
相关推荐
servlet与Struts action线程安全问题分析
2 线程模式方面的对比:Struts 1 Action是单例模式并且必须是线程安全的,因为仅有Action的一个实例来处理所有的请求。单例策略限制了Struts 1 Action能做的事,并且要在开发时特别小心。Action资源必须是线程安全的...
servlet与Struts action线程安全问题分析 <br>===================================================== Servlet的线程安全问题只有在大量的并发访问时才会显现出来,并且很难发现,因此在编写Servlet程序时要...
主要介绍了Struts中action线程安全问题解析,涉及实例代码,还是挺不错的,具有一定参考价值,需要的朋友可以了解下。
不同点 Action 类 线程模式 Servlet 依赖 可测性 捕获输入 表达式语言 绑定值到页面(view) 类型转换 校验 Action执行的控制 文档更详细……
Struts2和Struts1的不同 Action 类: 线程模式:
Struts2与Struts1的对比 对Action 类、线程模式、捕获输入、表达式语言、 绑定值到页面(view)、类型转换、Action执行的控制 等等读做了解析.
struts2.doc struts 1 和struts 2比较 Action 类 线程模式 Servlet 依赖 可测性 捕获输入 表达式语言 绑定值到页面(view) 类型转换 校验 Action执行的控制 等 自己总结的一些东西
清晰的介绍了Struts 2.0框架的工作流程,Action线程安全,程序入口,配置文件。
struts1.2 Action是单例模式的并且必须是线程安全的,因为仅有一个Action的实例来处理所有的请求。 单例策略限制了Struts1.2 Action能做的事情,并且开发时特别小心。Action资源必须是线程安全的或同步的。 struts...
基于Action类,线程模型,servlet依赖,易测性,接受输入,表达式语言等的比较
1) 在Action实现类方面的对比 2) 线程模式方面的对比 3) Servlet依赖方面的对比...
Struts2的处理流程: • 客户端产生一个HttpServletRequest的请求,该请求被提交到一系列的标准过滤器(Filter)组建链中(如ActionContextCleanUp:它主要是清理当前线程的ActionContext、Dispatcher,...
java 框架面试题 Action是不是线程安全的?如果不是 有什么方式可以保证Action的线程安全?如果是,说明原因 struts1不是,struts2是;
标准struts的action是非线程安全(很多简历上写着"精通Struts"的高手们都忽视的问题) 不熟悉: struts标签库没怎么用过(常用jstl) 国际化只学过,没用过.停留在demo级别 validation.xml(配置文件写多了,有时候也...
几十道面试题 如:strust的。 Action是不是线程安全的?如果不是 有什么方式可以保证Action的线程安全?如果是,说明原因 。。。。。
b)根据Web.xml配置,请求首先经过ActionContextCleanUp过滤器,其为可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助(SiteMeshPlugin),主要清理当前线程的ActionContext和Dispatcher;c)请求经过插件...