users@jersey.java.net

Dependency injection jersey + grizzly

From: spudtm <geeth.demel_at_gmail.com>
Date: Mon, 27 Sep 2010 12:07:15 -0700 (PDT)

Hi guys

I am trying out jersey for a set of REST services and having a similar
problem with regard to the dependency injection with jersey 1.4 (If I use
1.0.3 then, there are no errors).

I want to use Abdera ContentHelper in my resources (i.e., '_at_Context
ContentHelper contentHelper' at the top level of the class declaration) for
easy atom feed entity write. When I run my resources from eclipse, the
dependencies are injected and if I run my mvn project from command line as
'mvn exec:java' it still works, but when I create a complete jar of the
project (including all the dependencies) and execute the jar file (i.e.,
java -jar <jar file>) it throws up an exception which says

 "
SEVERE: The following errors and warnings have been detected with resource
and/or provider classes:
  SEVERE: Missing dependency for field:
com.sun.jersey.atom.abdera.ContentHelper <MyClass>.contentHelper
Sep 23, 2010 7:55:53 PM com.sun.grizzly.http.servlet.ServletAdapter
doService
SEVERE: service exception:
Throwable occurred: com.sun.jersey.spi.inject.Errors$ErrorMessagesException
...
"

This is of course jerser+grizzly not been able to get hold of an instance of
ContentHelper.

Following an example
(http://jersey.576304.n2.nabble.com/Simplest-Dependency-Injection-with-Jersey-td3393338.html#a5564545),
I have create an injector (See the bottom of the query) and load it up with
my grizzly server as follows.
 jsa.addInitParameter( "com.sun.jersey.config.property.packages",
                          "rs.csd.abdn.ac.uk.resources;" +
                          "rs.csd.abdn.ac.uk.providers");

From the output of the jersey/grizzly I can see that the class is picked.

Can you shed any light on this? The dependencies I am using are

1) jersey-core
2) jersey-server
3) jsr311-api
4) grizzly-servlet-webserver
5) jersey-atom-abdera
6) jaxb-api

Thanks
- Geeth

Injector class
========
package rs.csd.abdn.ac.uk.resources.injector;

import java.lang.reflect.Type;

import javax.annotation.Resource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.ext.Provider;

import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;

@Provider
public class ResourceInjector implements InjectableProvider<Resource, Type>
{
  public Injectable getInjectable(ComponentContext cc, Resource r,Type t)
  {
    final Object value = get(r);
    
    return new Injectable()
            {
              public Object getValue()
              {
                return value;
              }
            };
  }

  public ComponentScope getScope()
  {
    return ComponentScope.Singleton;
  }

  private Object get(Resource r)
  {
    try
    {
        Context ctx = new InitialContext();
        try
        {
          return ctx.lookup(r.name());
        }
        catch (NamingException ex)
        {
            return ctx.lookup("java:comp/env/" + r.name());
        }
    }
    catch (Exception ex)
    {
        throw new RuntimeException(ex);
    }
}
}
-- 
View this message in context: http://jersey.576304.n2.nabble.com/Dependency-injection-jersey-grizzly-tp5576514p5576514.html
Sent from the Jersey mailing list archive at Nabble.com.