Spring4gwt, or Spring’s harmony for GWT


I’ve had an idea itching at me for awhile now, which started shortly after building a complex enterprise application using GWT connected to Spring services on the server. There are countless blogs out there describing various ways to wire up Spring with GWT, and Google’s own Gin is quite adequate for client-only dependency injection. But what I’d really like is something that ties this all together, and in the true spirit of GWT, makes it easy for a seasoned Java developer to apply proven, consistent DI practices in both the client and server using the Spring knowledge they likely already have.

My vision is basically this:


@Component
public class SomeGWTComponent {

	// Some other client-side component
	@Autowired
	private MyWidget widget;

	// A remote service proxy to a server-side Spring service
	@Autowired
	private MyService service;

	public void doSomething() {

		widget.doClientStuff();

		...

		service.doServerStuff("data", new AsyncCallback<String>() {

				public void onFailure(Throwable caught) {
					...
				}

				public void onSuccess(String result) {
					...
				}
			});
		}
	}
}

It’s important that MyService is a vanilla Spring service on the server – no extension of RemoteServlet required – and that MyWidget is any other GWT component. Injection should occur when the component is created, potentially with varying scopes just like on the server, and potentially also with @PostConstruct support for code to be executed after injection, leaving the constructor available for any pre-injection initialization. And finally, as much work as possible should be done during GWT compile minimize the run-time cost.

The approaches to GWT lookup/injection I’ve seen out there have at least one the following gaps:

  • They’re client-only (Gin)
  • They’re largely run-time, and slow (MVC helpers in component libraries like GXT)
  • They involve SpringMVC or other unnecessary server-side complexity (most blog guides)

So I’m going to try to bring this altogether in one simple library with the following goals:

  • Simplicity: Access Spring services from the client through a generic exporter; no service-specific mappings in web.xml, forced type hierarchy (services should be existing Spring POJO’s), or dependency on SpringMVC. In other words, when a new Spring service is added to the server it should require zero configuration changes to be accessible in the client if desired.
  • Consistency: Client-side dependency injection with native Spring look and feel; freedom to inject private members, via constructor, or via getters/setters with same Spring annotations and conventions that are already familiar.
  • Performance: Do all the heavy lifting with GWT generators to keep the client lean.

Behold, spring4gwt where I’ll do my best to make this a reality! The initial version captures objective #1, bringing together a generic servlet for exposing Spring services without turning each into its own RemoteServlet mapped in web.xml. Up next: accessing those services with Spring DI in the client!

If you like this entry please share it using the links below!
  • Digg
  • Reddit
  • StumbleUpon
  • del.icio.us
  • Technorati
  • Google Bookmarks
  • LinkedIn
  • Facebook
  • Yahoo! Buzz
  • Twitter

, , , ,

  1. #1 by José at July 22nd, 2009

    Very nice. Looking forward to future releases.

  2. #2 by Kalyan at August 23rd, 2009

    Hello,
    I was looking to do exactly what you have in mind. But not being successful after looking at your project on google code.
    I detailed the problem here.

    http://spring4gwt.blogspot.com/

    If you could help me with this problem.. that’ll be great.

    Thanks
    Kalyan

  3. #3 by Dustin at August 23rd, 2009

    Kalyan :

    Hello,
    I was looking to do exactly what you have in mind. But not being successful after looking at your project on google code.
    I detailed the problem here.

    http://spring4gwt.blogspot.com/

    If you could help me with this problem.. that’ll be great.

    Thanks
    Kalyan

    This does look like a class file version issue. I just downloaded the posted spring4gwt-0.0.1.jar and ran a javap -verbose SpringGwtRemoteServiceServlet to make sure and it was built with target 1.5 so it should work fine with the jdk you have selected. I also built the demo projects with Java 5 (on Windows) and was able to run them correctly. Is there any other place in your Eclipse project that you may have selected Java 6 for compilation of server-side files? You could also try building the spring4gwt jar from source just to see if it works (build file specifies source/target 1.5, but I’ve only tested on Windows and Linux).

  4. #4 by Niel at September 17th, 2009

    Nice blog post. I have been working on GWT and Spring integration using DWR, but I will definitely being giving this a try.

(will not be published)
  1. No trackbacks yet.