My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GoogleAppEngine  

Guice20
Updated Oct 16, 2011 by sberlin

Using Guice with Google App Engine

You can use Guice with to write modular applications for Google App Engine.

Supported Builds

Google App Engine support requires Guice 2 (with or without AOP), plus the guice-servlet extension.

Setup

Servlet and Filter Registration

Configure servlets and filters by subclassing ServletModule:

package com.mycompany.myproject;

import com.google.inject.servlet.ServletModule;

class MyServletModule extends ServletModule {
  @Override protected void configureServlets() {
    serve("/*").with(MyServlet.class);
  }
}

Injector Creation

Construct your Guice injector in the getInjector() method of a class that extends GuiceServletContextListener. Be sure to include your application's servlet module in the list of modules.

package com.mycompany.myproject;

import com.google.inject.servlet.ServletModule;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.Guice;
import com.google.inject.Injector;

public class MyGuiceServletContextListener extends GuiceServletContextListener {

  @Override protected Injector getInjector() {
    return Guice.createInjector(
        new MyServletModule(),
        new BusinessLogicModule());
  }
}

Web.xml Configuration

You must register both the GuiceFilter and your subclass of GuiceServletContextListener in your application's web.xml file. All other servlets and filters may be configured in your servlet module.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 
   xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5"> 
  <display-name>My Project</display-name>
  
 <filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>com.mycompany.myproject.MyGuiceServletContextListener</listener-class>
  </listener>
</web-app>

WAR Layout

Ensure the AOP alliance, Guice, and Guice servlet jars are in the WEB-INF/lib directory of your .war file (or www directory):

  www/
      WEB-INF/
          lib/
              aopalliance.jar
              guice-servlet-snapshot.jar
              guice-snapshot.jar
              ...
          classes/
              ...
          appengine-web.xml
          web.xml
Comment by JasonThr...@gmail.com, May 28, 2011

Should probably add this to the docs above:

App Engine Log Errors

App Engine will throw a java.lang.reflect.InvocationTargetException when your App Engine projects starts because it tries to register a finalizer. This is not allowed in the App Engine environment, and it is benign to see this error: Guice will continue working normally. See: http://code.google.com/p/google-guice/issues/detail?id=488

Note that App Engine's log output will register these errors at the INFO level as:

I 2011-05-28 22:08:58.194
com.google.inject.internal.util.$FinalizableReferenceQueue$SystemLoader loadFinalizer: Not allowed to access system class loader.
I 2011-05-28 22:08:58.258
com.google.inject.internal.util.$FinalizableReferenceQueue <init>: Failed to start reference finalizer thread. Reference cleanup will only occur when new references are created.
java.lang.reflect.InvocationTargetException
	at com.google.appengine.runtime.Request.process-00f20a346f2e6269(Request.java)
...
Comment by dougande...@gmail.com, Feb 5, 2012

Now that Guice 3.0 is out, is App Engine compatible with Guice 3.0?

Comment by vec...@gmail.com, Mar 12, 2012

yes, I use guice 3.0 with latest GAE version

Comment by kartik...@gmail.com, Mar 29, 2012

Hey I am getting the following error

Uncaught exception from servlet java.lang.NoClassDefFoundError?: javax/inject/Provider

at com.google.inject.internal.MoreTypes?.canonicalizeForKey(MoreTypes?.java:81) at com.google.inject.Key.<init>(Key.java:119) at com.google.inject.Key.get(Key.java:212) at com.google.inject.spi.Elements$RecordingBinder?.bind(Elements.java:262) at com.google.inject.internal.InjectorShell?$RootModule?.configure(InjectorShell?.java:276) at com.google.inject.spi.Elements$RecordingBinder?.install(Elements.java:223) at com.google.inject.spi.Elements.getElements(Elements.java:101) at com.google.inject.internal.InjectorShell?$Builder.build(InjectorShell?.java:133) at com.google.inject.internal.InternalInjectorCreator?.build(InternalInjectorCreator?.java:103)
I have followed all the instructions as is. Can you tell me where I am going wrong.

Comment by project member cgruberatgoog@gmail.com, Mar 29, 2012

If you're using JSR-330 annotations (javax.inject) then you need to include that Jar as well, or it won't be in the classpath as you see above.


Sign in to add a comment
Powered by Google Project Hosting