---
title: "struts1.x源码赏析－action是线程安全性讨论\u3000"
author: "jakoes"
date: "2010-07-09T11:59:35Z"
tags: ["Bean", "Java", "Servlet", "Spring", "Struts"]
categories: ["编程语言"]
slug: "struts1-x-action"
draft: false
lastmod: "2010-07-09T11:59:35Z"
id: 709007
url: "https://jakoes.iteye.com/blog/709007"
---

经过对struts1.x源码的研读发现：
<span style="color:red">struts1.2获取action的方式是单例的，所有的action都被维护在一个hashMap里，当有请求到达时，先根据action的名称去hashMap里查找要请求的Action是否已经存在，如果存在，则直接返回hashMap里的action。如果不存在，则创建一个新的Action实例。</span>


下面我们来分析一下源码：

请求到达ActionServlet时，首先会到达doGet()或doPost()方法，而ActionServlet转给了process()方法进行统一处理



```java
    public void doPost(HttpServletRequest request,
               HttpServletResponse response)
        throws IOException, ServletException {

        process(request, response);

    }
```

 



而process()方法又会转给processor的process()方法进行处理


```java
    protected void process(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

        ...
        processor.process(request, response);

    }
```

 


processor的process()方法里经过一系列处理后，最后通过processActionCreate方法来返回一个具体的action实例



```java
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这个方法里去一窥究竟吧：
１、先获取类名 
２、根据类名去一个名为actions的map里查寻实例是否已经存在
３、如果存在，则直接返回
４、如果不存在，则创建一个新实例
５、把创建好的action放到map里备用


```java
  
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();//１、先获取类名 
        ...
        Action instance = null;
        synchronized (actions) {

            // Return any existing Action instance of this class
            instance = (Action) actions.get(className);//２、根据类名去map里查寻实例是否已经存在
            if (instance != null) {
                return (instance); //３、如果存在，则直接返回
            }

            // Create and return a new Action instance
            //４、如果不存在，则创建一个新实例
            instance = (Action) RequestUtils.applicationInstance(className)

            instance.setServlet(this.servlet);
            actions.put(className, instance);//５、把创建好的action放到map里
        }
        ...
        return (instance);

    }
```

 

我们再来看看actions的定义：


```java
    /**
     * 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.x里的action是单例模式的，供多个请求共享。所以不是线程安全的。
其数据都通过actionForm进行传递。
因此，<span style="color:red">action实例里面不应该声明带有状态的实例变量</span>。