This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Spring and the fly likes Struts2.1.8 Spring3.2.1 anootated app with autowired, gives Service as NULL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Struts2.1.8 Spring3.2.1 anootated app with autowired, gives Service as NULL" Watch "Struts2.1.8 Spring3.2.1 anootated app with autowired, gives Service as NULL" New topic
Forums: Spring Struts
Author

Struts2.1.8 Spring3.2.1 anootated app with autowired, gives Service as NULL

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Hi guys,

Spent the past day or so trying to set up a little Struts2, Spring3 (eventually Hibernate4) app with Annotations eg @autowired
but am having a little trouble.

For some reason my Action is always dropping to teh error.jsp, because my TestService is NULL.
Just wondering if anyone can see an obvioius reason why this is the case. I think I have included everything I need to below...








I'm sure it's going to turn out to be something stupid...

Thanks in advance

KS
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1672
    
    7

TestAction where you are Autowiring in your service is not a spring bean. Autowiring can only occur in Spring managed beans.

If you plan to use Struts 2 with Spring then I suggest you take a look at using the Struts 2 plugin for Spring.
http://struts.apache.org/development/2.x/docs/spring-plugin.html


[How To Ask Questions][Read before you PM me]
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Interesting.

I have the spring-struts-plugin.jar but guess I'm not using it properly.

Another interesting read I just found is this, not tried any of it yet though.

http://www.ibm.com/developerworks/library/j-sr2/index.html

Cheers for the input.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1672
    
    7

That is a really old thread and probably pertains to Struts 1. Struts 1 support is built into Spring. If you want to use Struts 2 then you should use the plugin I referred to earlier.


I don't see the plugin in your listed dependencies.

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.8</version>
</dependency>


Have a look at this tutorial
http://www.dzone.com/tutorials/java/struts-2/struts-2-example/struts-2-spring-plugin-integration-1.html

Note you don't need to wire your bean up in XML you are fine with the component scan like you have your bean name by convention will be "testService". However you can override this by giving a different name to the annotation @Service("myService")

Here was how someone else did it
http://stackoverflow.com/questions/9446176/struts2-spring-plugin-dependency-injection-with-annotated-actions-does-not-work

I think you should carefully read over the fist link I gave you again. I think you are missing something in the configuration. I don't use Struts 2 and Spring so I can provide much more assistance with this. I did add this to our Struts forum in case someone there has more experience integrating these 2 technologies.
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Arh yes it works with the struts2-spring.2.3.8.jar I had the 'reverse' jar spring-struts2.jar

Cheers!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts2.1.8 Spring3.2.1 anootated app with autowired, gives Service as NULL