SIDEBAR
»
S
I
D
E
B
A
R
«
Non invasive GWT and Spring integration (reloaded)
July 17th, 2009 by P.G.Taboada

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 http://code.google.com/p/spring4gwt/.


17 Responses  
  • P.G.Taboada writes:
    August 11th, 200910:37at

    Sorry, having troubles with XML code snippets and the layout… Hope to get it fixed soon…

  • Alec Missine writes:
    August 24th, 200920:36at

    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, http://minetats.com , 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.

    Thanks,
    Alec Missine

  • Michael writes:
    August 25th, 200907:11at

    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”).

    Thanks,
    Michael

  • pgt » Best Practices For Architecting Your GWT App writes:
    September 18th, 200915:36at

    […] Non invasive GWT and Spring integration (reloaded) […]

  • Sam Brodkin writes:
    October 14th, 200913:55at

    Thanks P.G. This is indeed the most straightforward way to go.

    I have uploaded a full implementation of the ideas in this article here:

    http://code.google.com/p/gwt-spring-starter-app/

    It’s simply the greeting app you get when you use the GWT webAppCreator but then modified according to your article.

  • P.G.Taboada writes:
    November 1st, 200917:12at

    Before you start using GWT-RPC, give it a second thought by having a look here:

    http://52.90.157.123/2009/09/18/best-practices-for-architecting-your-gwt-app/

  • Constantine writes:
    December 17th, 200917:47at

    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.

  • P.G.Taboada writes:
    December 18th, 200912:12at

    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.

  • Hans writes:
    August 25th, 201010:19at

    Anyone found a solution to use this in Spring 2.1 yet?

  • Rashad Tycer writes:
    September 5th, 201020:11at

    netter Artikel. Ich würde aber mal ein bisschen deine Website für Suchmaschienen optimieren, die findet man sehr schwer über google.

  • Daniele writes:
    September 15th, 201011:56at

    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\AutoinjectingRemoteServiceServlet.java:[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?

    Cheers

    Daniele

  • Daniele writes:
    September 15th, 201012:34at

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

    So sorry

    Cheers

    Daniele

  • iteezy writes:
    November 21st, 201016:31at

    Looks simpler than the toCollage-net project.

  • Renato writes:
    June 9th, 201106:22at

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

  • dmnida writes:
    January 30th, 201206:02at

    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

  • Marek Czarnecki writes:
    January 31st, 201402:35at

    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
    metadata-complete=”true”
    and all is well again.

    Thanks again. Simplicity is beauty!

  • P.G.Taboada writes:
    January 31st, 201403:07at

    You are welcome. You could just change @Ressource to @Qualifier and get the same result.


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="">

SIDEBAR
»
S
I
D
E
B
A
R
«
»  Substance:WordPress   »  Style:Ahren Ahimsa
Bitnami