struts2.0中Action的对象生命周期详解!!(转)

原文出处:http://blog.csdn.net/wxy_g/article/details/2071662

有很多人问Struts2.0中的对象既然都是线程安全的,都不是单例模式,那么它究竟何时创建,何时销毁呢?


这个和struts2.0中的配置有关,我们来看struts.properties

 

### if specified, the default object factory can be overridden here
### Note: short-hand notation is supported in some cases, such as "spring"
###       Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here  
struts.objectFactory = spring

如果我们使用的是com.opensymphony.xwork2.ObjectFactory ,老实说,我也没有研究过,xwork有一套像spring一样的IOC机制,小巧而简洁,有兴趣的朋友可以去研究下。struts2.0中的Action默认就是使用这种工厂模式的,我们来看

 <action name="index" class="hdu.management.action.IndexAction">
        <result name="success">/input.jsp</result>
        <result name="testFTL" type="freemarker">/ftl/test.jsp</result>
    </action>

class属性必须写类的全名,通过这种方式配置后,action对象的生命周期到底怎么样,你就认命吧,反正你就记住xwork有一个对象池,它会自己分配的,应对每次客户端的请求,它都会创建一个新的实例,至于这个实例何时销毁,由XWORK来控制。


接下来,我们用spring来控制action的生命周期,关于action和spring的集成,我这里就不累述了。

    <action name="index" class="index">
        <result name="success">/input.jsp</result>
        <result name="testFTL" type="freemarker">/ftl/test.jsp</result>
    </action>

这里的class是spring配置文件中bean的id

我们来看看spring文档中关于生命周期这个章节

Table 3.4. Bean scopes

Scope        Description

singleton

Scopes a single bean definition to a single object instance per Spring IoC container.

prototype

Scopes a single bean definition to any number of object instances.

request

Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

session

Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

global session

Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.

转载于:https://www.cnblogs.com/m3Lee/p/3925560.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值