users@jersey.java.net

Re: SV: Simplest Dependency Injection with Jersey

From: spudtm <geeth.demel_at_gmail.com>
Date: Thu, 23 Sep 2010 13:12:48 -0700 (PDT)

Hi Paul

I am trying out jersey for a set of REST services and having a similar
problem with regard to the dependency injection.

I want to use '_at_Context ContentHelper contentHelper' for easy atom feed
entity write. When I run my resources from eclipse, the dependencies as
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
...
"

Following your example, 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?

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/Simplest-Dependency-Injection-with-Jersey-tp3393338p5564545.html
Sent from the Jersey mailing list archive at Nabble.com.