Use Spring with GWT dispatch
September 16th, 2009 by P.G.Taboada

From the GWT dispatch site:

Inspired by Ray Ryan’s Best Practices For Architecting Your GWT App session at Google I/O 2009, „gwt dispatch“ is an implementation of the ‚command pattern‘ discussed at the beginning of the video.

The project uses Gin in the frontend and Guice in the backend. Guice defines a very nice dependency injection framework, and as such, competes partially with the Springframework. But the Springframework is by far more than dependency injection, and I don’t want to miss any of its features in the backend.

So I took a look at the gwt dispatch sources, and decided to use Spring to setup the server side of the dispatch service. GWT dispatch extensively uses constructor injection. This works perfectly with Spring, but not with plain servlets. So I needed to rewrite the DispatchServiceServlet a little…

Instead of using constructor injection, I use setter injection. This way the servlet can be simply added to the web.xml. The servlet lifecycle initializes servlets before they are service-ready. I use the initialization callback method to autowire the servlet instance.

Here is my web.xml:

I am using annotation based configuration in my project, so the spring configuration file is very concise:

The component scan will add any properly annotated class to the spring application context. Obviously, the classes in the GWT dispatch project are not properly annotated. So I created the classes in my projects that are needed by the DispatchService implementation:

The action handler registry is the place where the ActionHandlers are kept. I used the collection autowire feature to get a list of all handlers registered in the Spring application context.

The last step is to add ActionHandlers to the Spring context. For obvious reasons I prefer to keep my action handlers „Spring free“ – that’s why I created my own annotation to identify action handlers:

I am not sure if I need to setup the retention policy – I should have a look at that…

All I need to do now is to add handlers to any package below the server package. The Spring component scan will automatically find the handlers and add them to the Spring application context and further inject them to the handler registry.

Here is a simple ActionHandler:

I am very happy with this approach as it allows me to add new action (command) handlers without any configuration overhead. Simply dropping the implementation somewhere in the server package will do all the magic.

12 Responses  
  • pgt » Best Practices For Architecting Your GWT App writes:
    September 18th, 200910:24at

    […] Use Spring with GWT dispatch » September 18th, […]

  • Jeff writes:
    November 4th, 200920:52at

    Thanks for this. It is a HUGE help!

  • pgt » GWT is not the future but the present of web development writes:
    November 5th, 200912:36at

    […] Use Spring with GWT dispatch […]

  • Adrian writes:
    November 11th, 200916:09at

    @Jeff and pgt

    it would be very useful for me and others reading this if you can provide your eclipse project files for us to examine and study the changes required to fully convert this to a spring based approach.. anyway, do you still include the gin and guice jar files on your project? you see, i’m trying to modify the hivedevelopment mvp sample project to your approach. thanks!

  • Pietro writes:
    November 18th, 200919:45at

    I second Adrian. Thanks.

  • P.G.Taboada writes:
    Dezember 9th, 200917:03at

    HI, have a look here:

    Just uploaded a sample project. Hope this helps!

  • Matthew writes:
    Februar 9th, 201000:14at

    How does your handler access the Request/Response/Session objects?

  • Raj writes:
    Februar 26th, 201010:16at

    I am getting the following error

    Deferred binding result type ’net.customware.gwt.dispatch.client.service.DispatchServiceAsync‘ should not be abstract

    Please help

  • that’s great… » GWT wiring via Spring… writes:
    September 10th, 201002:38at

    […] to wire your GWT components is ridiculously easy. I ran into some issues, but thanks to a post from pgt, I was able to work through […]

  • zxiko writes:
    Oktober 16th, 201011:15at

    Is This production ready?

  • Normurod writes:
    November 23rd, 201009:27at

    how to access ServletRequest object in gwt-dispatch ActionHandler?

  • P.G.Taboada writes:
    November 23rd, 201012:24at

    @Normurod: have a look at section

    3.5.4 Request, session, and global session scopes

    in the Spring reference documentation here

    Either you are happy with scoped beans and forget about manually managing your http-scoped bean, or you dig deeper into the RequestContextListener, it is a Servlet 2.4+ listener that exposes the request to the current thread, through both LocaleContextHolder and RequestContextHolder.


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

»  Substance:WordPress   »  Style:Ahren Ahimsa