简体   繁体   English

Spring管理的Struts 2 Action类bean的适当范围应该是什么?

[英]What should be the appropriate scope for Struts 2 Action class beans managed by Spring?

In a Spring managed Struts 2 application, the HttpServletRequest objects are stored as a global variable inside the Action classes. 在Spring托管的Struts 2应用程序中, HttpServletRequest对象作为全局变量存储在Action类中。 After doing some research I found that by default Struts Action classes are thread safe as they are instantiated for every request, but in the scenario described above, the Struts Action classes are configured as Spring beans without mentioning the bean scope(So by default they are singletons). 经过研究后,我发现默认情况下Struts Action类是线程安全的,因为对于每个请求都将它们实例化,但是在上述情况下,Struts Action类被配置为Spring Bean,而没有提及bean范围(因此默认情况下,它们是单身人士)。 I think this is a recipe for a disaster, so I was searching for a fix and I found the following in the official Struts 2 Spring integration example below: 我认为这是一场灾难的秘诀,因此我在寻找解决方法,并在下面的官方Struts 2 Spring集成示例中找到了以下内容:

<bean id="editService" class="org.apache.struts.edit.service.EditServiceInMemory"/>

<bean id="editAction" class="org.apache.struts.edit.action.EditAction" scope="prototype">

    <property name="editService" ref="editService" />

</bean>

Here the scope is set to prototype but I thing it should be request as the action class needs to instantiated for every Http request. 这里的作用域设置为原型,但我想应该将其请求,因为需要为每个Http请求实例化操作类。 Please tell me whether my understanding is correct and if it is the correct solution, other than instantiating the request objects as local variables. 除了将请求对象实例化为局部变量之外,请告诉我我的理解是否正确以及是否正确的解决方案。

I had found the following question in stack overflow but I want to know in terms of Struts 2: Spring Request and Prototype Scope? 我在堆栈溢出中发现了以下问题,但是我想了解Struts 2: Spring Request和Prototype Scope?

This post explains pretty clearly how the two work: Spring Request and Prototype Scope? 这篇文章清楚地解释了这两个工作原理: Spring Request和Prototype Scope?

I would say that if your app only uses simple actions, it really doesn't matter which you use. 我想说的是,如果您的应用程序仅使用简单的操作,那么使用什么就无关紧要。 If you are using filters or struts interceptors and want to hold state between different steps in the request pipeline, then you must use request scoped beans. 如果使用过滤器或Struts拦截器,并且希望在请求管道的不同步骤之间保持状态,则必须使用请求范围的Bean。 Otherwise it again doesn't matter. 否则再次没有关系。

I would probably use prototype based beans when request based ones weren't absolutely necessary. 当基于请求的bean不是绝对必要时,我可能会使用基于原型的bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM