Reputation: 16238
In the strange, strange dependency injection world of Jersey, you can include an AbstractBinder
(but maybe not just a Binder
) as one of the objects in the return value of your Application
's getSingletons()
method.
That AbstractBinder
can then call various bind()
methods from within its configure()
method, which Jersey, but no other JAX-RS implementation, is guaranteed to call—and hence you can link implementations to interfaces, which lets you do a semblance of dependency injection from that point forward in your application. That is, once you've done that, then injection points within, say, your resource classes will be "filled" by the implementations you've bound.
(You can also do this with factory classes, so that you bind a particular factory method's return value to a contract it implements.)
OK, fine.
Jersey also lets you place a Class
that implements Feature
into the return value of your Application
's getClasses()
method. Fine. This Feature
class will be instantiated by HK2 under the covers—make a mental note of that!—and its configure(FeatureContext)
method will be called at some point. At that point, the Feature
may register some additional stuff by calling FeatureContext#register()
and handing it whatever it wants to register.
(Presumably this is all a fairly complicated façade on top of HK2's DynamicConfiguration
machinery.)
Anyway, since a Feature
is instantiated by HK2 (remember?), it follows that you can @Inject
things into its constructor, or have injection points elsewhere in the class. That's cool! Then HK2 can draw upon all the services it knows of to fill those injection points. Very neat.
Ah, but the question is: what is the state of the HK2 world at this point? What services can be injected into a Feature
implementation instantiated as part of A JAX-RS Application
's startup sequence?
If your Application
's getSingletons()
method returns an AbstractBinder
implementation that binds a FooImpl
to Foo
in Singleton
scope in its configure()
method, can your Feature
—"registered" by including its class in the return value of your Application
's getClasses()
method—then inject a Foo
?
Upvotes: 0
Views: 420
Reputation: 544
I think it is important to have your Foo
interface binding proxied, i.e:
new AbstractBinder() {
@Override
protected void configure() {
bind(Foo.class)
.proxy(true)
.to(FooImpl.class)
.in(Singleton.class);
}
}
then dependency injection will be insensitive to the instatination order.
Upvotes: 1