Monday, September 27, 2010

GWT: Developer Mode and Spring Contex Listener

I could not get the usual Spring web context listener to work with GWT 2.0.4 in developer mode, running within Eclipse.

Usually the context listener will load the bootstrap Spring files and then parse the classpath to do auowiring, etc. Running the GWT application in Developer mode failed to parse the classpath so any @Autowired annotations threw exceptions with no bean found error message.

The same Spring files loaded fine when loaded "old school way" - IE remove the web context loader listener and use a manual creation of the Spring application context using the same XML files.

Code Change:

In order to get the Developer mode working within Eclipse, I followed what other monkeys had done and nastily copy & paste the GWT class used to start up Developer mode (which is essentially a Jetty-specific class) and made one small change to get it working for Spring classpath parsing.

The class is called com.google.gwt.dev.shell.jetty.JettyLoader
located here: gwt-src-2010-09-02/trunk/dev/core/src (where gwt-src-2010-09-02 is the root of where the GWT code was checked out to from Subversion).

I copy the whole class as a new class called SpringJettyLoader and changed it as follows:
private final ClassLoader bootStrapOnlyClassLoader = new ClassLoader(null) {};
to
private final ClassLoader bootStrapOnlyClassLoader = Thread.currentThread().getContextClassLoader();

I also had to copy the additional class called JettyNullLogger and include it with my new loader class to reduce hassles.

Running GWT Web Application:

A change is required to the Eclipse Run Configuration used to run your GWT web application. Open up the Run Configuration.
On the Server (2nd) tab, uncheck the "Run built-in server" option.
On the Arguments tab, change the current arguments to include your new class as the -server option.
-noserver 
-remoteUI "${gwt_remote_ui_server_port}:${unique_id}" 
-startupUrl PropertyWeb.html 
-server com.tradecraft.property.web.server.jetty.SpringJettyLauncher 
-logLevel INFO 
-war /home/aisling/development/workspaces/workspace2/property-web/war com.tradecraft.property.web.PropertyWeb

Eclipse Classpath:

I was using m2eclipse to do my dependency resolution for me within Eclipse. This ended up being more trouble than it was worth as I kept getting other dependencies includes that I didn't expect. This especially caused issues with Hibernate 3.3.5.FINAL and JPA 1.0 and 2.0.

I ended up putting all my libs in the usual WEB-INF/lib directory and all the other Eclipse projects specifically pointed to those jars.

Eclipse Source Folders Additional Note:

Removing m2eclipse from the projects, left the classes output pointing to /target/classes which is fine but then I attempted to change all the output directories to /bin. The previously added source folders contain to point to the old location and you wonder why nothing works.

It is cleaner to remove all the source folders within each Eclipse project and then re-add them as they then point to where you expect them to.

No comments:

Post a Comment