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!
#1 by José on July 22, 2009 - 2:02 am
Very nice. Looking forward to future releases.
#2 by Kalyan on August 23, 2009 - 9:04 am
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 by Dustin on August 23, 2009 - 7:50 pm
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 by Niel on September 17, 2009 - 7:32 pm
Nice blog post. I have been working on GWT and Spring integration using DWR, but I will definitely being giving this a try.
#5 by Jaen Swart on June 4, 2010 - 1:54 am
Hi,
Did you stop development of spring4gwt or are you just busy at the moment
I see the last update is July 2009
#6 by Dustin on June 11, 2010 - 8:57 pm
I’ve been busy, but also with a final release of Google Gin I’ve realized that most of the advanced features I was thinking about are much better solved by Gin. So the basic RPC integration that’s there is all I’ve ended up needing and like most open-source project my desire to put more into it is driven entirely by need.
I’d highly recommend Gin for anyone implementing fully-featured dependency management in their GWT app, not to mention it works seamlessly with Guice for unit testing.