Home > Atmosphere > Using Google Guice with Atmosphere

Using Google Guice with Atmosphere

Starting with Atmosphere 0.5, you can now use Google Guice to configure Atmosphere

IMG_0159.JPG

Google Guice support is enabled by referencing the Guice filter GuiceFilter and an application specific ServletContextListener that extends from GuiceServletContextListener in the web.xml. For example, the web.xml may be as follows:

   <web-app>
     <listener>
       <listener-class>org.atmosphere.samples.guice.GuiceChatModule</listener-class>
     </listener>
     <filter>

       <filter-name>Guice Filter</filter-name>
       <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
     </filter>
     <filter-mapping>
       <filter-name>Guice Filter</filter-name>

       <url-pattern>/*</url-pattern>
     </filter-mapping>
   </web-app>

and the application specific servlet context listener may be as follows:

public class GuiceChatConfig extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new GuiceChatModule());
    }
}

with

public class GuiceChatModule  extends ServletModule{

    @Override
    protected void configureServlets(){
        Map params = new HashMap();
        params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "org.atmosphere.samples.guice");
        serve("/chat*").with(AtmosphereGuiceServlet.class, params);
    }
}

That’s it. You can download the previously Atmosphere-Jersey sample, this time Guice enabled from here. For any questions or to download Atmosphere, go to our main site and use our Nabble forum (no subscription needed) or follow us on Twitter and tweet your questions there!

// // technorati:

About these ads
Categories: Atmosphere
  1. December 18, 2009 at 9:58 am

    If you bind your Guice components, such as root resource classes, in the GuiceChatModule then it is not necessary to include the parameter for package scanning. See the following link for more details:

    https://jersey.dev.java.net/nonav/apidocs/latest/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: