1

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!

Roman C
  • 49,761
  • 33
  • 66
  • 176
Justin
  • 6,031
  • 11
  • 48
  • 82
  • from the responses thus far, i guess the bottom line is using Spring plugin for Struts is the most common way of Spring/Struts integration. there's no sense in going out of my way to avoid using the plugin for standard use-case – Justin Aug 24 '12 at 13:56
  • That will be the best option and you already decided to use Spring so why to have 2 different DI, one for S2 and one for other things – Umesh Awasthi Aug 24 '12 at 14:23

2 Answers2

1

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.

Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204
1

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Dave, I don't understand your comment regarding Guice. I'm not using Guice, just Spring and javax.inject.Inject for DI rather than Spring's legacy Autowired annotation. Your larger point is understood, no need to go out of my way NOT to use the Spring plugin for Struts for most common Struts/Spring integration use cases. – Justin Aug 24 '12 at 14:02
  • @Justin S2 uses an old, internal version of Guice for its injection. – Dave Newton Aug 24 '12 at 14:04