為什么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是原型模式