问题
I'm trying to make my action classes singletons. The point is to make action classes real controllers in MVC pattern. Especially when implementing REST controllers the scope of the controller could be extended to the life of the application. Like in Spring framework the controller is put to the default scope by default, the default scope in Spring is singleton.
Struts2 has also a default scope, and it's also singleton. I want to put my action classes to this scope and remove them from the value stack.
How could I modify Struts2 framework to make my actions like a controller in Spring? I know that I could just delegate management of action classes to Spring, but I can't use a default scope, and Struts2 container is still keep running. I can't remove it from the framework, because it's not pluggable.
So, piggybacking around Spring and Struts2 container I can't make my action classes singletons because Struts2 instantiate them and put to the value stack. That is my question: if I choose the container between Struts and Spring to put my action classes to default scope, how could I tell Struts2 framework not to put them to the value stack?
回答1:
Assuming you're using XWork's DefaultActionInvocation
implementation, it's done there, by the init
method. Pulling that out is a bit of a pain because it's layered underneath action proxies and action proxy factories in both XWork and S2.
That said, I'd be very hesitant at making a change like this; it has system-wide implications and is counter to essentially everything about XW/WW/S2.
(Unrelated, but singleton nature isn't what defines a controller, it's the responsibilities that define what a component is.)
来源:https://stackoverflow.com/questions/35802110/how-to-remove-actions-from-the-value-stack