struts2.0中Action的对象生命周期详解!!
有很多人问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.
是不是一目了然?
当然我们要使用request、session等,必须在web.xml中配置
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
准备好这些之后,我们来对request这个scope进行下测试
<beans>
<bean id="index" class="hdu.management.action.IndexAction"
scope="request"
destroy-method="destroy"
init-method="init"/>
</beans>
配置好后,发现每次刷新页面,都会建立一个新的实例,运行完后就销毁这个实例,这个效果和默认的是一样的,只是我们这个运行完后会立即销毁,而默认的不是立即销毁,由xwork优化销毁
如果设置为session,其实相当于ejb中的状态bean,对于每个session来说用的都是同一个实例,当然,一旦把浏览器关闭,或者超时,对象就会销亡。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wxy_G/archive/2008/01/29/2071662.aspx
分享到:
相关推荐
03 Struts 2.0的Action讲解 04 在Struts 2.0中国际化(i18n)您的应用程序 05 转换器(Converter)——Struts 2.0中的魔术师 06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器...
Struts2.0视频教程,struts2.0中文教程,Struts2.0视频教程,struts2.0中文教程,
struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0
Struts 2.0的Action讲解 在Struts 2.0中国际化(i18n)您的应用程序 转换器(Converter)——Struts 2.0中的魔术师 在Struts 2.0中实现表单数据校验(Validation) Struts 2的基石——拦截器(Interceptor) 在...
Struts 2.0的Action讲解 com.opensymphony.xwork2.ActionSupport类
sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0
struts2.0的数据校验框架struts2.0的数据校验框架struts2.0的数据校验框架struts2.0的数据校验框架
有Struts 1.x经验的朋友都知道Action是Struts的核心内容,当然Struts 2.0也不例外。不过,Struts 1.x与Struts 2.0的Action模型很大的区别 本文档对struts2.0 action进行了详细介绍
struts2.0jar包 struts2.0包 struts2.0源文件
JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0...
03 Struts 2.0的Action讲解 04 在Struts 2.0中国际化(i18n)您的应用程序 05 转换器(Converter)——Struts 2.0中的魔术师 06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器...
Javascript特效大全(上册)Spring.Security.3Struts2.0中文教程struts2标签详解w3school层叠样式表程序员常用JavaScript特效正则表达式系统教程
Struts 2.0中英文API文档,非常有价值的学习资料!!!对学习Struts2.0非常有帮助!!!!
Struts2.0框架技术详解。本文描述了Struts2的框架技术详解,希望对大家有所补益。
Struts2.0的各种配置详解Struts2.0的各种配置详解Struts2.0的各种配置详解Struts2.0的各种配置详解Struts2.0的各种配置详解Struts2.0的各种配置详解Struts2.0的各种配置详解
此为Struts2.0最新Jar包,方便各位用于Struts2.0的开发.
struts2.0中struts.xml配置文件详解
struts 2.0 详细配置 struts 2.0 详细配置 struts 2.0 详细配置