Struts1.x单例模式分析

本文分析了Struts1.2中的Action处理方式,指出Action被设计为单例模式,从HashMap中获取或创建实例。由于是单例,Action在多线程环境下不安全,数据通过ActionForm传递,不建议在Action实例中声明有状态的变量。通过源码解析,详细阐述了Action实例的创建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

struts1.2获取action的方式是单例的,所有的action都被维护在一个hashMap里,当有请求到达时,先根据action的名称去hashMap里查找要请求的Action是否已经存在,如果存在,则直接返回hashMap里的action。如果不存在,则创建一个新的Action实例。

结论: struts1.x里的action是单例模式的,供多个请求共享。所以不是线程安全的。其数据都通过actionForm进行传递。因此,action实例里面不应该声明带有状态的实例变量

下面我们来分析一下源码:

请求到达ActionServlet时,首先会到达doGet()或doPost()方法,而ActionServlet转给了process()方法进行统一处理

1.public void doPost(HttpServletRequest request,   
2.           HttpServletResponse response)   
3.    throws IOException, ServletException {   
4.  
5.    process(request, response);   
6.  
7.}
1.public void doPost(HttpServletRequest request,  
2.           HttpServletResponse response)  
3.    throws IOException, ServletException {  
4.  
5.    process(request, response);  
6.  
7.}

而process()方法又会转给processor的process()方法进行处理

1.protected void process(HttpServletRequest request, HttpServletResponse response)   
2.    throws IOException, ServletException {   
3.  
4.    ...   
5.    processor.process(request, response);   
6.  
7.}
1.protected void process(HttpServletRequest request, HttpServletResponse response)  
2.    throws IOException, ServletException {  
3.  
4.    ...  
5.    processor.process(request, response);  
6.  
7.}

processor的process()方法里经过一系列处理后,最后通过processActionCreate方法来返回一个具体的action实例

1.public void process(HttpServletRequest request,   
2.                        HttpServletResponse response)   
3.        throws IOException, ServletException {   
4.  
5.        ...   
6.  
7.        // Create or acquire the Action instance to process this request   
8.        Action action = processActionCreate(request, response,

 mapping);   
9.        if (action == null) {   
10.            return;   
11.        }   
12.  
13.     ...   
14.  
15.    }  
1.public void process(HttpServletRequest request,  
2.                        HttpServletResponse response)  
3.        throws IOException, ServletException {  
4.  
5.        ...  
6.  
7.        // Create or acquire the Action instance to process this request  
8.        Action action = processActionCreate(request, response, mapping);  
9.        if (action == null) {  
10.            return;  
11.        }  
12.  
13.     ...  
14.  
15.    }  

那我们就到processActionCreate这个方法里去一窥究竟吧:
1、先获取类名
2、根据类名去一个名为actions的map里查寻实例是否已经存在
3、如果存在,则直接返回
4、如果不存在,则创建一个新实例
5、把创建好的action放到map里备用

2.protected Action processActionCreate(HttpServletRequest request,   
3.                                         HttpServletResponse response,   
4.                                         ActionMapping mapping)   
5.        throws IOException {   
6.  
7.        // Acquire the Action instance we will be using (if there is one)   
8.        String className = mapping.getType();//1、先获取类名    
9.        ...   
10.        Action instance = null;   
11.        synchronized (actions) {   
12.  
13.            // Return any existing Action instance of this class   
14.            instance = (Action) actions.get(className);//2、根据类名去map里查寻实例是否已经存在   
15.            if (instance != null) {   
16.                return (instance); //3、如果存在,则直接返回   
17.            }   
18.  
19.            // Create and return a new Action instance   
20.            //4、如果不存在,则创建一个新实例   
21.            instance = (Action) RequestUtils.applicationInstance(className)   
22.  
23.            instance.setServlet(this.servlet);   
24.            actions.put(className, instance);//5、把创建好的action放到map里   
25.        }   
26.        ...   
27.        return (instance);   
28.  
29.    }  
1.    
2.protected Action processActionCreate(HttpServletRequest request,  
3.                                         HttpServletResponse response,  
4.                                         ActionMapping mapping)  
5.        throws IOException {  
6.  
7.        // Acquire the Action instance we will be using (if there is one)  
8.        String className = mapping.getType();//1、先获取类名   
9.        ...  
10.        Action instance = null;  
11.        synchronized (actions) {  
12.  
13.            // Return any existing Action instance of this class  
14.            instance = (Action) actions.get(className);//2、根据类名去map里查寻实例是否已经存在  
15.            if (instance != null) {  
16.                return (instance); //3、如果存在,则直接返回  
17.            }  
18.  
19.            // Create and return a new Action instance  
20.            //4、如果不存在,则创建一个新实例  
21.            instance = (Action) RequestUtils.applicationInstance(className)  
22.  
23.            instance.setServlet(this.servlet);  
24.            actions.put(className, instance);//5、把创建好的action放到map里  
25.        }  
26.        ...  
27.        return (instance);  
28.  
29.    }  

我们再来看看actions的定义:

1./**  
2. * The set of Action instances that have been created and  
3. * initialized, keyed by the fully qualified Java class name of the  
4. * Action class.  
5. */  
6.protected HashMap actions = new HashMap();  ```
1./** 
2. * The set of Action instances that have been created and 
3. * initialized, keyed by the fully qualified Java class name of the 
4. * Action class. 
5. */  
6.protected HashMap actions = new HashMap();

结论:
==struts1.x里的action是单例模式的,供多个请求共享。所以不是线程安全的。其数据都通过actionForm进行传递。因此,action实例里面不应该声明带有状态的实例变量==

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄鸭and小黑鸭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值