Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowiring Struts Action Classes with Spring

Tags:

spring

struts

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?

like image 914
David Pugh Avatar asked Dec 29 '25 03:12

David Pugh


1 Answers

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.

like image 183
Tomasz Nurkiewicz Avatar answered Dec 31 '25 00:12

Tomasz Nurkiewicz