3
votes

I have a question about spring and struts. Currently I have spring injecting my struts action classes for me.

I was experimenting and trying to get Spring to inject my Struts action classes for me using autowiring.

I have my spring applicationContext config file scanning the base package that the action class is in using context:component-scan base-package="my.package", and im using @Component annotation at the action classes class level. Im also using @Qualifier("myActionClass") at the same action classes class level. Im not configuring the action class as a Spring bean in applicationContext.

Then in my struts.xml config file, while configuring my action class, instead of giving the fully qualified package and class name, I use the @Qualifier annotation name "myActionClass".

This doesnt work though. If in my applicationContext config file, configure my action class as a spring bean, get rid of the @Component and @Qualifier annotation on the action class, and in struts.xml, put the action classes Spring bean id for the class, then Spring injects my action class for me and everything is dandy. Only, this isnt using Autowiring the action class, and thats what I was testing.

Anyone know if autowiring using context:component-scan base-package to scan your packages for your action classes so you dont have to configure them in applicationContext is possible?

2

2 Answers

3
votes

Everything is explained in Spring documentation: Apache Struts 1.x and 2.x.

I am not sure whether you are using Struts 1 or 2. For Struts 1 you had to add Spring plugin to Struts configuration (I know it works). In Struts 2 all actions are created by Spring hence they are fully capable of Spring injection like all other beans.

0
votes

Struts 2 seems to rely on there being a spring bean with the same spring bean-name matching the action class name (full name with package). You can specify the bean name in the @Component annotation, and it's also possible to make a global user-defined bean naming strategy so you can avoid adding this information to all your beans