Update 7/9/2010 – MyEclipse for Spring 8.6 now generates full ready-to-run GWT applications based on MVP and UI Binder in minutes. Just point the scaffolding wizard at your database tables, Java beans, or JPA Entities. You can learn more about it from the Generating Enterprise Class GWT applications for Spring post that I wrote on Genuitec blog. Or, you can keep reading my original post…
In the previous post I introduced a new GWT extension for Skyway Builder that automatically makes Spring @Services accessible as GWT RPC services and accelerates the development of GWT events that can be used with a GWT event bus. This blog post will provide a high-level description of the implementation of the extension, and in the next blog post I will dive into the implementation details .
I’m not going to spend a lot of time explaining how spring4gwt works. Please refer to the project site for detailed description of spring4gwt. There are essentially five steps for making Spring @Services accessible as GWT RPC Services using spring4gwt.
Implementation requirements for producing GWT RPC services from Spring Services:
- Copy Spring4GWT library into project
- Modify web application context file (web.xml) – add spring4gwt servlet and servlet mapping
- Create a a GWT callback interface for the Spring @Service – each service operation should be reflected in callback interface
- Modify the service interface – add import statements for GWT remote service; add @RemoteServiceRelativePath annotation; extend Remote Service
- Modify service context file (Spring) - since GWT clients replace JSP and controllers, the component scan that is typically located in the web context file needs to be moved to the service context file, otherwise the Spring framework won’t be able to find other components that the service might be reliant upon.
Except for the new GWT callback interface, all the relevant artifacts are already generated by the Spring DSL. The GWT extension will just need to customize the generation of these artifacts to support GWT RPC using spring4gwt. The callback interface, on the other hand, is a new artifact, and the Spring DSL will need to be extended to generate it.
Here’s what you need to know about the Spring DSL. The Spring DSL defines 10 abstractions, which include Project, Controller, Component, Service, Domain Object, Data Access Object, Operation, Named Query, Exception and Action. Each Spring DSL abstraction will generate primary and secondary artifacts, and the generated artifacts are typically Java code or XML configuration files, although you really can generate anything that can be emitted with JET.

During the course of application development using the Spring DSL a developer will define instances of Spring DSL artifacts, and Skyway Builder will generate the primary and secondary artifacts according to the configuration of the Spring DSL artifacts.

For the GWT RPC functions the two relevent Spring DSL abstractions are: Project and Service. According to the Spring DSL there are 6 artifact definitions for a Service, and the following diagram shows the most relevant ones -plus- the new artifact definition (GWT Interface).

For the Service interface the GWT extension will override the default code generation template that’s provided by Skyway Builder, and the new template will emit code required in the service interface (see requirement #3). For the GWT callback interface (requirement #4) the GWT extension will extend the definition of a Service by adding a new artifact definition and contributing a new code generation template.
Now that requirements 3 and 4 are out of the way, let’s proceed to requirements 2 and 5. These requirements will be handled in a very similar manner. The web.xml and Spring context files are generated by the Spring DSL Project abstraction. There are a lot of artifacts generated from a Project, so the following diagram only shows the most relevant ones.

The GWT extension will override the code generation templates for web.xml and service context file. The new templates will add the additional configurations needed to support GWT RPC.
In the grand scheme of things this exercise was pretty trivial because all the required steps are handled by the GWT extension by just overriding an existing template or contributing a new artifact definition to an existing Spring DSL element. It’s worth noting that this is just the tip of iceberg when it comes to extending the Spring DSL. If needed you could extend Spring DSL element to capture more metadata, which presumably would be used to drive the code generation. You could also create entirely new Spring DSL elements with their own meta-data and code generation templates.
The implementation overview of the GWT RPC function is complete, and now we will proceed to the GWT Events function. For this function I needed to decide whether or not I should create an entirely new Spring DSL element for Events or whether I could use a pre-existing Spring element. For the sake of simplicity I decided to use the existing Spring DSL Component element because it already captures all the metadata that I need for generating Events. Components have a name, which I’ll use as the event name, and components have variables, which I’ll use for defining the event payload. Here’s the definition of a Component -plus- the new GWT event.

The process for adding support for generating GWT events from a Component is nearly identical to the process of generating the callback interface from a Service. The GWT extension defines a new artifact definition for Spring DSL Components, and a new code generation template will be used to produce GWT events.
As you can see it was relatively simple to customize the Spring DSL, and the basic recipe described in this post can be applied to countless development scenarios.
If you are familiar with Skyway Template Projects, you may be wondering how this process is different than Skyway Template Projects. Both let you customize the code generation templates, so why was an Spring DSL extension used instead of a Skyway Template Project. While Spring DSL extensions and Skyway Template Projects share some functionality, you can do a lot more with the Spring DSL extensions. I already pointed out that this recipe is just the tip of the iceberg when it comes to extending the Spring DSL. While Template Projects let you customize the templates, they don’t let you add new artifact definitions (which was one of the requirements of the GWT integration), extend the Spring DSL with additional metadata, or define entirely new Spring DSL abstractions. If you are only interested in overriding pre-existing code generation templates for pre-existing Spring DSL abstrations, then the Skyway Template Project is your best bet. However Skyway Template Projects weren’t adequate for the GWT-RPC or GWT events requirements.
In the next post I will describe the implementation of the extension in more detail.