3

I am developing a GWT-Spring-Hibernate project and I want to use Spring Autowired annotation in GWT Service Servlet but my autowired annotated service is not injected. it is null. Is there a configuration detail that I missed?

I add

<context:annotation-config />
<context:component-scan base-package="com.org" />

to my ApplicationContext.xml and I have annotated my service as @Service("myService")

@Autowired
MyService myService;  // This is null so WHY?
firstthumb
  • 4,627
  • 6
  • 35
  • 45

3 Answers3

4

You need to "autowire" your RPC servlets during initialization. Take a look here http://code.google.com/p/gwt-spring-starter-app/

nevermind
  • 51
  • 3
2

Well, the class where the @Autowired annotation resides should also be in the spring context (i.e. annotated with @Component), but I doubt it will work if it is a GWT (i.e. client-side) class.

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

Is class you're trying to inject your service into actually a bean declared in Spring context? It should be, auto-wiring won't work otherwise.

It can either be declared explicitly or, provided that it's somewhere within your "com.org" hierrarchy it will be detected automatically IF it's annotated as @Component or one of other stereotypes provided by Spring.

ChssPly76
  • 99,456
  • 24
  • 206
  • 195
  • Package for MyService is com.org and annotation by @Service stereotypes so I think it is enough to inject – firstthumb Nov 19 '09 at 08:33
  • I wasn't asking about MyService. A quote from above: "class you're trying to **inject** your service **into** ". – ChssPly76 Nov 19 '09 at 08:45
  • I calling MyService in GWT Servlet so you say that I have to declare GWT Servlets in Spring Context? Hmmm. I don't think so but I will try this project http://code.google.com/p/spring4gwt/. Thanks for your advise – firstthumb Nov 19 '09 at 08:54
  • I'm not saying you should declare GWT servlet in Spring context. I am, however, saying that Spring autowiring will **NOT** work unless both the bean being injected and the bean it's injected into are present in Spring context. I haven't tried spring4gwt; they may be doing behind the scenes processing of GWT servlets to make this possible. – ChssPly76 Nov 19 '09 at 09:02