Non invasive GWT and Spring integration (reloaded)

New project layout
New project layout

Here is my overdue update to my Non invasive GWT and Spring integration blog post from early 2008. Since then we have had the GWT 1.6 and GWT 1.7 releases:

One of the biggest changes to GWT 1.6 is a new project structure. The old output format has been replaced by the standard Java web app "expanded war" format, and the actual directory name does default to "/war". Note that the war directory is not only for compiler output; it is also intended to contain handwritten static resources that you want to be included in your webapp alongside GWT modules (that is, things you'd want to version control).

As a matter of fact, now we finally can (must) manage the web.xml file ourselves:

Projects with server-side code (GWT RPC) must configure a web.xml file at /war/WEB-INF/web.xml. This web.xml file must define and publish any servlets associated with the web application.

This is a little pain for really much gain, and that's why I am writing this post after all...

Starting a Springframework ApplicationContext in a Java EE compiient web-application is well known and documented in the Springframework reference:

There aren't many beans in my spring configuration files as I am using annotation based configuration in my apps. Here a sample spring confuguration taken from one of my sample projects:

So this is (despite of the missing log4j.xml config) the configuration setup I am using on GWT 1.6 and GWT 1.7 projects in order to bootstrap an application context.

The next step is to implement the dependency injection of spring managed beans into my GWT RPC serverside implementation. I will be using the annotation based config provided by the Springframework.

This is how the dependency injection in my GWT RPC services looks like:

EncodingServiceImpl is the server side GWt RPC implementation, while EncodingService is the GWT service I am setting up. The managed bean in my spring configuration implements IEncodingService (and uses the great jasypt lib). I am using the annotations to configure the dependency injection.

All I have to do now, is "autowire" my RPC servlet during initialization. Since this is not going to be the only GWT RPC service needing dependency injection I introduced the AutoinjectingRemoteServiceServlet class:

There is not much to say about this class. During the initializiation of the RPC Servlet (remember: GWT uses servlets as the foundation technology on the server side implementation of GWT RPC. Being so, the GWT RPC service implementation "lives" in the same lifecycle as any other servlet. I am using the initialization step to get a reference to the ApplicationContext. After that I use the autowire bean method to autowire the servlet automatically.

If you prefer to directly expose spring beans, you should have a look at

17 thoughts on “Non invasive GWT and Spring integration (reloaded)”

  1. Hello, P.G.,

    Thank you for the post. I like the way you use Spring auto-injecting with GWT RPC, am going to give it a try. On my website, , I have another example on how one could use GWT and Spring together – a menu-driven web application. I use commands on both client and server sides. On the server, I use ServiceLocatorFactoryBean as well as Spring Security (with a GWT-driven login page). Please take a look if interested.

    Alec Missine

  2. Hi Alec,

    nice example application. Would it be possible to download the whole example as a (Eclipse) project? This would be more comfortable than downloading the individual files (btw clicking on “spring-security.xml” shows an alert “not implemented”).


  3. Thanks for a simple yet effective method! Though, I had to fix it a bit – using autowireBeanProperties instead of autowireBean, otherwise my services didn’t get populated.

  4. Hi Constantine,

    you are welcome, I am glad it fits you need.
    From the Spring API:

    autowireBean( Object existingBean )

    Populate the given bean instance through applying
    after-instantiation callbacks and bean property
    post-processing (e.g. for annotation-driven injection).

    autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)

    Autowire the bean properties of the given bean instance by name or type.

    Well, “autowireBean” worked properly for me, can’t tell why it did not work for you.

  5. Hi, I’ve found your solution very clean and sharp.
    In theory, though.
    I’ve tried it in a larger environment where we have to do gwt-rpc-spring, and it complains in compilation phase(!!) that:

    [INFO] Compilation failure
    \workspace-sgr\xx\yy\service\common\[26,46] cannot access org.springframework.context.ApplicationContext
    class file for org.springframework.context.ApplicationContext not found
    AutowireCapableBeanFactory beanFactory = ctx.getAutowireCapableBeanFactory();

    as if there were permission issues accessing org.springframework.context.ApplicationContext (quite obviously spring-context.jar IS in the classpath..)

    Any ideas? Did it ever happen to you?



  6. Never mind, I’ve dumbly left runtime for the spring-context.jar in my pom.xml while it has to be compile..

    So sorry



  7. This has saved me so much frustration, mate! Thanks a lot for this. GWT and Spring now work together seamlessly.

  8. Is there a way to integrate GWT 2.1 into Existing Spring 3.0 MVC.
    I was unable to find any example. Is there any mvn archetype for this kind of project

  9. Just wanted to say this solution to Spring GWT integration is _still_ helping me deliver solutions :-) Thank you!

    One problem; when upgrading to a deployment to Tomcat 7.0 + – I suddenly started getting NameExceptions.

    Tremendously puzzled, and then discovered that in more recent Tomcat containers, the container tries to fulfill @Resource annotations before Spring gets a look-in.

    Solution is to upgrade the web.xml to web_app_3.0.xsd with attribute
    and all is well again.

    Thanks again. Simplicity is beauty!

Leave a Reply

Your email address will not be published. Required fields are marked *