users@jersey.java.net

Re: [Jersey] Injected into hell

From: Martin Probst <mail_at_martin-probst.com>
Date: Thu, 14 Aug 2008 11:48:56 +0200

> Yes, this is exactly what I'm doing. I'll try the SNAPSHOT on my code.

I'm happy to report that it does work. However I'd like to suggest
that the API might benefit from some simplification. It's probably
nice to have access to the complex cases, but in most cases you
probably just need two things: inject a global instance (singleton-
style), or inject a per-request instance. In the first case, you
probably already have the instance there, and in the second case you'd
probably want to supply a nested class that will return the instance
on every request.

E.g.:

   @Override
   protected void initiate(ResourceConfig rc, WebApplication wa) {
     rc.addSingletonInstance(MyAnnotation.class, MyType.class,
myInstance);
     rc.addPerRequestInjectable(MyAnnotation.class, MyOtherType.class,
new Injectable<MyOtherType>() { get() { return myInstance2; } });
   }

I agree that the method names suck, and you could probably make this
even easier by just assuming that people will use the "Context"
annotation, or the "Inject" annotation (where's the difference
anyways?). This reduces the number of classes needed by two or one,
depending on use case, and I personally think it's a lot more
straightforward.

Regards,
Martin