users@jersey.java.net

Injected into hell

From: Martin Probst <mail_at_martin-probst.com>
Date: Fri, 8 Aug 2008 11:07:57 +0200

Hi,

I'm still trying to get Jersey to inject a class I provide into
resource classes. I'm failing in more and more obscure ways. By now,
I'm completely puzzled by the various @Context, @Inject annotations,
Injectables and InjectableProviders ... so I'm willing to go back all
the way, apparently I must have missed a crucial thing that has
changed somewhen before 0.9.

My problem appears to be simple, at least to me. All I want to say is
"dear Jersey, I have this type, called org.hibernate.Session, and in
case you're creating a Resource, and it has a field of that type, and
it's annotated with @Inject (or whatever), please inject this instance
which you can get from this class". This shouldn't be difficult, right?

So this is the current state of the mess that used to be my application:

   private final class HibernatingServletContainer extends
ServletContainer {
     @Override
     protected void initiate(ResourceConfig rc, WebApplication wa) {
       persistence = new Persistence();
       rc.getProviderInstances().add(new SessionProvider());
       super.initiate(rc, wa);
     }
   }

   private class SessionProvider implements
InjectableProvider<Context, Session> {

     public Injectable<Session> getInjectable(ComponentContext ic,
Context a, Session c) {
       return new Injectable<Session>() {

         public Session getValue(HttpContext context) {
           return persistence.getSession();
         }
       };
     }

     public Scope getScope() {
       return Scope.PerRequest;
     }
   }

... but Jersey doesn't even seem to learn about my SessionProvider.

Also, there is apparently no example using the Injectable or Inject
stuff - any pointers?

Thanks,
Martin