Struts2 + Spring, is the Spring Plugin required?

339 Views Asked by At

I'm replacing the service tier in an existing older Struts2 project with Spring service beans developed for another project.

I'd like to just @Inject these service beans into my Action classes.

Is it required to use Struts' Spring Plugin? Or can I add Spring into my Struts web application like I would any other (ContextLoaderListener, applicationContext.xml, context:component-scan)?

Am I missing some reason why the Struts Spring plugin helps me in another way?

Many thanks!

2

There are 2 best solutions below

0
On BEST ANSWER

Well you can do the most of the things what you have described in your question as Services layer is completely out of view for the S2 and Struts2 do not care how you are creating your Service layer instances and other things.

Benefits i am seeing of using Struts2-Spring plugin is to delegate creation of Struts2 related things to Spring like Action classes creation,Interceptors,Results etc.

My Suggestion is to use the plugin as you are going to use the Spring in your application so its very good and flexible as well powerful to use the power of Spring DI to create required objects needed by S2 else S2 will use its own data creation factory to create framework component.

2
On

Why wouldn't you use the Spring plugin?

It's essentially invisible, uses Spring to create your actions (including injecting other Spring beans), etc.

Guice's @Inject doesn't know anything about Spring beans, AFAIK, so you'd be naming classes manually, they'd be instantiated via normal Java/Guice mechanisms, wouldn't be injected with their own Spring dependencies (unless you did it manually, or via AOP, or whatever).

You'd also need to use non-Spring mechanisms for doing injection in testing, which is fine, but unless you provide more details regarding your usecase, I don't really see a reason to bypass the functionality the Spring plugin provides out-of-the-box.