Home

Wednesday, August 17, 2011

Spring 3 on Google App Engine

So last week I got the inspiration to try out Google App Engine after reading about the new Java support. I searched around to find some resources on getting Spring running on App Engine, but the resources were surprisingly scarce. Google points you to their "autoshoppe" example, which is a little incomplete in terms of setup and what libraries you need. Ironically, Spring Source's post about App Engine was talking about using Groovy, not Spring. Finally I found a blog entry by Sikeh. So I got a nice Hello World up and running on Spring 2, thanks to Sikeh, but I wasn't satisfied yet. I wanted Spring 3. Spring 2 is so... 2008. So I started bringing in some Spring 3 (M2) jars, played around a little more, did some Googling, and finally got Spring 3 up and running.

Here's how you'll need to setup the directory structure:

  1. Setup a Google App Engine project (I used the Eclipse plugin)
  2. Download Spring 3.0.0.M2 (or the latest version)
  3. Copy the following jar files from Spring into the /war/WEB-INF/lib directory:
    org.springframework.beans-3.0.0.M2.jar
    org.springframework.context-3.0.0.M2.jar
    org.springframework.core-3.0.0.M2.jar
    org.springframework.expression-3.0.0.M2.jar
    org.springframework.web-3.0.0.M2.jar
    org.springframework.web.servlet-3.0.0.M2.jar
  4. Spring also depends on the following jars, which you can search for around the internet, or download from my Spring Example git repository:
    antlr-3.0.1.jar
    asm-2.1.jar
    asm-commons-2.1.jar
    commons-logging-1.1.1.jar
  5. Finally, you'll need to rename the commons-logging-1.1.1 jar to something else, like commons-logging.jar (thanks to Martin for this tip!)

Next, you'll need to configure your web.xml for Spring. In web.xml I added a catch-all mapping to my Spring dispatcher:

<servlet>
<servlet-name>dispatcherservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<load-on-startup>1load-on-startup>

servlet>
<servlet-mapping>
<servlet-name>dispatcherservlet-name>
<url-pattern>/*url-pattern>
servlet-mapping>

WARNING: If you use a catch-all (/*) url-pattern, the application will not work in development mode (locally), but will work on the AppEngine server. I believe this is a known bug.

For my dispatcher-servlet.xml: (also in /war/WEB-INF/)

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="example.controllers" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" />

beans>

You obviously want to change the base-package to whatever package you want. I use the InternalResourceViewResolver, and put all my views in /WEB-INF/views/. Finally, here's my (uninteresting) applicationContext.xml:

xml version="1.0" encoding="UTF-8"?>


<beans>

beans>

Now we're all done with setup! Just create a controller and a view. I made a simple hello.jsp view that outputs the request parameter "name". Here's the controller:

package example.controllers.hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

@RequestMapping("/hello/{name}")
public String hello(@PathVariable String name, Model model) {
model
.addAttribute("name", name);
return "hello/hello";
}
}

So there you have it. Spring up and running on Google App Engine. You can see it in action athttp://springexample.appspot.com/, or you can get the source at http://github.com/idris/spring-example-gae/. For now, this is all just a Hello Wold. I'll post more functionality later... Feel free to suggest topics in the comments!

No comments:

Post a Comment