简体   繁体   English

是否可以每次为struts 1.x动作类创建一个新实例?

[英]Can we create a new instance for struts 1.x action classes each time?

The action classes of struts 1.x is inherently not thread safe since the struts caches the action classes and uses it for other requests too. struts 1.x的操作类本质上不是线程安全的,因为struts缓存了操作类并将其用于其他请求。

Is there any way we can configure struts 1.x action classes in such a way that a new instance of the action class is created each time ? 有没有什么方法可以配置struts 1.x动作类,每次都创建一个动作类的新实例?

If it is possible to do so, are there any downsides to doing it ? 如果可能的话,这样做有没有不利之处?

Ultimately, of course the answer is yes: you can get the complete source for Struts 1 and twist it into something it was not intended to be. 最终,当然答案是肯定的:你可以获得Struts 1的完整源代码并将其转换为不打算成为的东西。

Should you? 应该 Almost certainly not. 几乎肯定不是。 If you decide to undertake this, start with RequestProcessor.processActionCreate . 如果您决定采用此方法,请从RequestProcessor.processActionCreate开始。

I'm with Brad, but would be a bit stronger: what you're going to try to do is a bad idea. 我和布拉德在一起,但会更强一些:你要做的是一个坏主意。 Use session context, application context, thread locals, synchronization, etc. as the framework was intended to be used. 使用会话上下文,应用程序上下文,线程本地,同步等,因为该框架是要使用的。

By moving outside the framework's intent, you open yourself up to an amount of technical risk that should concern you. 通过超出框架的意图,您可以承担一定的技术风险,这些风险应该与您有关。 Nothing in Struts 1 was tested with an action-per-request, because that's not how the framework was built. Struts 1中的任何内容都没有使用每个请求的操作进行测试,因为这不是框架的构建方式。 It may work. 它可能会奏效。 It may fail spectacularly. 它可能会失败。 It may look like it's working. 它可能看起来像是在工作。 For awhile. 一阵子。 Until it isn't, and you're screwed. 直到它没有,你被搞砸了。

IMO it'd be more effective to use the framework as designed. IMO会更好地使用设计的框架。 What problem are you trying to solve? 你想解决什么问题? There's likely a better solution that wouldn't require at least a full release cycle to vet the modified framework's functionality. 可能有一个更好的解决方案,至少不需要完整的发布周期来审查修改后的框架的功能。

Don't do it. 不要这样做。 As you've stated they're not thread safe. 正如你所说,它们不是线程安全的。

If you need to hold state for the user, store the data in a database and a lookup key value in the user's HTTP_SESSION. 如果需要为用户保持状态,请将数据存储在数据库中,并将查找键值存储在用户的HTTP_SESSION中。 If (and only if) you need better performance you can introduce a caching strategy. 如果(并且仅当)您需要更好的性能,您可以引入缓存策略。

If you're trying to store some data just for each single request, and don't want to or can't use the HTTP_SESSION, you could look at using ThreadLocal 如果您只想为每个请求存储一些数据,并且不想或不能使用HTTP_SESSION,则可以考虑使用ThreadLocal

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

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