Package com.sun.jersey.guice.spi.container.servlet

Provides support for Guice-based Web applications.

See:
          Description

Class Summary
GuiceContainer A Servlet or Filter for deploying root resource classes with Guice integration.
 

Package com.sun.jersey.guice.spi.container.servlet Description

Provides support for Guice-based Web applications.

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>foo.MyGuiceConfig</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:
     package foo;
 
     import com.google.inject.Guice;
     import com.google.inject.Injector;
     import com.google.inject.servlet.GuiceServletContextListener;
     import com.google.inject.servlet.ServletModule;
     import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
     import foo.GuiceResource
     
     public class MyGuiceConfig extends GuiceServletContextListener {

         @Override
         protected Injector getInjector() {
             return Guice.createInjector(new ServletModule() {

                 @Override
                 protected void configureServlets() {
                     bind(GuiceResource.class);

                     serve("/*").with(GuiceContainer.class);
                 }
             }
         });
     }
 }
 
Notice that one class GuiceResource is bound and the GuiceContainer is declared in the serve method. Instances of GuiceResource will be managed according to the scope declared using Guice defined scopes. For example the GuiceResource could be as follows:
    @Path("bound/perrequest")
    @RequestScoped
    public static class GuiceResource {

        @QueryParam("x") String x;

        @GET
        @Produces("text/plain")
        public String getIt() {
            return "Hello From Guice: " + x;
        }
    }
 

Any root resource or provider classes bound by Guice will be automatically registered. It is possible to intermix Guice and non-Guice registration of classes by additionally using the normal Jersey-based registration mechanisms in the servlet context listener implementation. For example:

     package foo;

     import com.google.inject.Guice;
     import com.google.inject.Injector;
     import com.google.inject.servlet.GuiceServletContextListener;
     import com.google.inject.servlet.ServletModule;
     import com.sun.jersey.api.core.PackagesResourceConfig;
     import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
     import foo.GuiceResource
     import java.util.HashMap;
     import java.util.Map;
 
     public class GuiceServletConfig extends GuiceServletContextListener {

         @Override
         protected Injector getInjector() {
             return Guice.createInjector(new ServletModule() {

                 @Override
                 protected void configureServlets() {
                     bind(GuiceResource.class);

                     Map<String, String> params = new HashMap<String, String>();
                     params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "unbound");
                     serve("/*").with(GuiceContainer.class, params);
                 }
             }
         });
     }
 }
 

Any root resource or provider classes found in the package unbound or sub-packages of will be registered whether they be Guice-bound nor not.

Sometimes it is convienient for developers not to explicitly bind a resource or provider, let Guice instantiate, and let Jersey manage the life-cycle. This behaviour can be enabled for a resource or provider class as follows:

  1. a class constructor is annotated with Inject;
  2. the class is not explicitly bound in Guice; and
  3. the class is registered using a Jersey based registration mechanism, for example using package scanning registration.



Copyright © 2010 Sun Microsystems, Inc. All Rights Reserved.