大家都知道,struts1.2由于是线程安全的,每一个请求都去实例化一个action,造成大量并发时的资源浪费。
struts2在这一点上做了改进,每个action都是一个singleton,所有的请求都是请求同一个action实例。这样在一定程度上能节约资源,但又有安全问题。最常见的就是在action中声明有块状的实例变量,因为这一点是不被提倡的。如果一定要声明,那一定要加上同步块。
那么在spring mvc中的controller是不是线程安全的呢?答案是否定的。controller在默认情况下也是非线程安全的,我们来看看源码:
* @author John A. Lewis
* @author Juergen Hoeller
* @since 2.0
* @see ResourceAwareController
* @see EventAwareController
*/
public abstract class AbstractController extends PortletContentGenerator implements Controller {
[color=red]private boolean synchronizeOnSession = false;[/color]
由上面源码可知,controller默认是非安全的。
public void handleActionRequest(ActionRequest request, ActionResponse response) throws Exception {
// Delegate to PortletContentGenerator for checking and preparing.
check(request, response);
// Execute in synchronized block if required.
//只有synchronizeOnSession设置为true,才会同步处理请求
if (this.synchronizeOnSession) {
PortletSession session = request.getPortletSession(false);
if (session != null) {
synchronized (session) {
handleActionRequestInternal(request, response);
return;
}
}
}
handleActionRequestInternal(request, response);
}
只有手工设置controller的synchronizeOnSession值为true,才会被同步处理。
因此,我们在使用spring mvc 的contrller时,应避免在controller中定义实例变量。
##### 更正 ####################################
由于在下对struts1.x的理解也来自网络,给大家带来不便,还请见谅。
经过对struts1.x源码的研读发现:
struts1.2获取action的方式是单例的,所有的action都被维护在一个hashMap里,当有请求到达时,先根据action的名称去hashMap里查找要请求的Action是否已经存在,如果存在,则直接返回hashMap里的action。如果不存在,则创建一个新的Action实例。
下面我们来分析一下源码:
请求到达ActionServlet时,首先会到达doGet()或doPost()方法,而ActionServlet转给了process()方法进行统一处理
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}
而process()方法又会转给processor的process()方法进行处理
protected void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
...
processor.process(request, response);
}
processor的process()方法里经过一系列处理后,最后通过processActionCreate方法来返回一个具体的action实例
public void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
...
// Create or acquire the Action instance to process this request
Action action = processActionCreate(request, response, mapping);
if (action == null) {
return;
}
...
}
那我们就到processActionCreate这个方法里去一窥究竟吧:
1、先获取类名
2、根据类名去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);
}
我们再来看看actions的定义:
/**
* 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();
结论:
struts1.2获取action的方式是单例的,所有的action都被维护在一个hashMap里,当有请求到达时,先根据action的名称去hashMap里查找要请求的Action是否已经存在,如果存在,则直接返回hashMap里的action。如果不存在,则创建一个新的Action实例。
分享到:
相关推荐
AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析...
spring源码spring-framework-4.3.2.RELEASE
spring源码编译之后,各个项目可能缺失jar包 spring-cglib-repack-3.2.4.jar和spring-objenesis-repack-2.5.1.jar
官方源码 spring-framework-5.3.4.zip官方源码 spring-framework-5.3.4.zip
spring源码缺失jar。spring-cglib-repack-3.2.5.jar,spring-objenesis-repack-2.6.jar
官方原版源码spring-framework-5.1.4.RELEASE.zip
官方源码 spring-framework-5.2.15.RELEASE.zip官方源码 spring-framework-5.2.15.RELEASE.zip
官方源码 spring-framework-5.3.2.zip官方源码 spring-framework-5.3.2.zip
Spring源码缺失的spring-cglib-repack-3.2.6.jar和spring-objenesis-repack-2.6.jar
官方原版源码spring-framework-5.1.6.RELEASE.zip
Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单...
spring源码导入SpringObjenesis类报错解决,缺少jar包spring-cglib-repack-3.2.0.jar和spring-objenesis-repack-2.1.jar
Spring源码编译缺少的两个包:spring-cglib-repack-3.2.0.jar和spring-objenesis-repack-2.2.jar
Spring 源码分析文档----自用。
spring-data-redis-1.8.1.RELEASE-sources.jar(spring-data-redis-1.8.1.RELEASE-sources.jar()
spring-framework-4.2.5.RELEASE,spring4.2.5版源码。
官方原版源码spring-framework-4.3.22.RELEASE.zip
spring-security-web源码 所需jar包
spring4.0源码编译时缺失的两个jar包,spring-cglib-repack-3.1.jar,spring-objenesis-repack-2.1.jar
官方原版源码spring-framework-5.1.9.RELEASE.zip