|
||||||||||
| PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES | |||||||||
See:
Description
| Class Summary | |
|---|---|
| GuiceContainer | A Servlet or Filter for deploying root resource classes
with Guice integration. |
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 JerseyServletModule() {
@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. A instance of
module JerseyServletModule is created. This
module extends from ServletModule and
provides JAX-RS and Jersey bindings.
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 JerseyServletModule() {
@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 convenient for developers not to explicitly bind a resource or provider, let Guice instantiate, and let Jersey manage the life-cycle. This behavior can be enabled for a resource or provider class as follows:
Inject;
In other cases it is convenient to let Jersey instantiate and manage the life-cycle and let Guice perform injection. This behavior can be enabled for a resource or provider class as follows:
Inject;
|
||||||||||
| PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES | |||||||||