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

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

在 Spring 托管 Struts 2 应用程序中,HttpServletRequest 对象存储为 全局变量在 Action classes 中。 在做了一些研究之后,我发现默认情况下 Struts Action classes 是线程安全的,因为它们是为每个请求实例化的,但是在上述场景中,Struts Action classes 被配置为 Spring beans 而没有提及 bean 范围(因此默认情况下它们是单例)。我认为这是灾难的根源,所以我在寻找修复方法,并在下面的官方 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>

此处范围设置为原型,但我认为它应该是请求,因为操作 class 需要为每个 Http 请求实例化。请告诉我我的理解是否正确,是否是正确的解决方案,而不是将请求对象实例化为局部变量。

我在堆栈溢出中发现了以下问题,但我想根据 Struts 2 了解: Spring Request and Prototype Scope?

这个 post 非常清楚地解释了两者的工作原理:Spring Request and Prototype Scope?

我想说的是,如果您的应用仅使用简单的操作,那么使用哪个并不重要。如果您正在使用过滤器或 struts 拦截器并希望在请求管道中的不同步骤之间保持状态,那么您必须使用请求作用域 bean。否则它又没关系。

当基于请求的 bean 不是绝对必要时,我可能会使用基于原型的 bean。