struts1与struts2 Action重要区别

struts1 Acton是单例的,且线程安全的 . 下面是struts1.2的源码:

 

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();
 


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


01 protected Action processActionCreate(HttpServletRequest request,     

02                                          HttpServletResponse response,     

03                                          ActionMapping mapping)     

04         throws IOException {     

05      

06         // Acquire the Action instance we will be using (if there is one)     

07         String className = mapping.getType();//1、先获取类名      

08         ...     

09         Action instance = null;     

10         synchronized (actions) {     

11      

12             // Return any existing Action instance of this class     

13             instance = (Action) actions.get(className);//2、根据类名去map里查寻实例是否已经存在     

14             if (instance != null) {     

15                 return (instance); //3、如果存在,则直接返回     

16             }     

17      

18             // Create and return a new Action instance     

19             //4、如果不存在,则创建一个新实例     

20             instance = (Action) RequestUtils.applicationInstance(className)     

21      

22             instance.setServlet(this.servlet);     

23             actions.put(className, instance);//5、把创建好的action放到map里     

24         }     

25         ...     

26         return (instance);     

27      

28     }

 

 

 

struts2 Acton是多例的,且线程安全的 . 下面是struts2的源码:

 

Java代码

 

<bean id="saveUserAction" class="com.test.action.user.SaveUserAction" scope="prototype">   

    <property name="service" ref="userService"></property>   

</bean>  

 

注 : A. 另外说几句 ,当struts交由spring管理的时候 ,spring的bean配置默认是单例的 ,
如果action是有状态的 ,必须显示的配置为prototype

 

 

 

B . struts2 在struts1的基础上做了改进 ,对于struts2 ,每次请求过来都会new一个新的action , 所以说struts2的action是线程安全的 , 但同时也带来一个问题,每次都new一个action ,这样action的实例太多 , 在性能方面还是存在一定的缺陷的。struts2已经在方面做了很好的优化。

C. spring作用域:prototype
‍prototype作用域部署的bean,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)都会产生一个新的bean实例,相当与一个new的操作,对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个prototype bean的整个生命周期负责,容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法,而对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值