Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2014 Zach Melamed
   * 
   * Latest version available online at https://github.com/zach-m/tectonica-commons
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.tectonica.guice;
 
Servlet-listener for intercepting REST requests and pass them through Guice before they are actually served by Jersey. This allows you to inject instances into your JAX-RS annotated classes. This class can be used along with GuiceJsfInjector to also support injection into JSF ManagedBeans.

To use it, first make sure to include the following dependency in your pom.xml:

 <dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-guice</artifactId>
 </dependency>
 
Then, you need to extend this class and provide a constructor that would offer your own subclass of com.tectonica.jee5.GuiceRestModule, reflecting your configuration. At the minimum, this subclass needs to implement com.tectonica.jee5.GuiceRestModule.getRootPackage() or com.tectonica.jee5.GuiceRestModule.bindJaxrsResources(). For example:
 public class RestConfig extends GuiceListener
 {
  public RestConfig()
    {
       super(new GuiceRestModule()
       {
          @Override
          protected String getServingUrl()
          {
             return ...;
          }
 
          @Override
          protected String getRootPackage()
          {
             return ...;
          }
       });
    }
 }
 
Once implemented, register your listener with web.xml:
 <listener>
    <listener-class>com.example.api.RestConfig</listener-class>
 </listener>
 

Author(s):
Zach Melamed
 
 public class GuiceListener extends GuiceServletContextListener
 {
 	protected final AbstractModule module;
 	protected static Injector injector;
 
 	protected GuiceListener(AbstractModule customModule)
 	{
 		 = customModule;
 	}
 
 	protected Injector getInjector()
 	{
 		return ;
 	}

given a class, generates an injected instance. Useful when an API call is needed internally.
 
 	public static <T> T getInstance(Class<T> type)
 	{
		return .getInstance(type);
	}

given an injectable instance, injects its dependencies
	public static void injectMembers(Object instance)
	{
	}
New to GrepCode? Check out our FAQ X