Howdy, all-
How can I programmatically wire-up singleton instances for injection?
I want to control instantiating some dependencies outside of Jersey,
then make them available to Jersey so they will be automatically
injected into instances of other classes managed by Jersey.
For example:
// I want Jersey to automatically inject instances of Bar into my
resources
class Bar {}
// I want to control instantiation of Foo outside of Jersey, and have
Jersey delegate to Foo
// to access instances of Bar
class Foo {
private Bar bar;
public Foo(Bar bar) { this.bar = bar; }
public Bar getBar() { return bar; }
}
// This is how I want Jersey to make Bar available to my other
resources
@Provider
public class BarProvider implements Injectable<Bar>,
InjectableProvider<Context, Type> {
private Bar bar;
// ********************************************************
// BOOM! NO BUENO:
// SEVERE: The following errors and warnings have been detected with
resource and/or provider classes:
// SEVERE: Missing dependency for constructor public
BarProvider(Foo) at parameter index 0
// ********************************************************
public BarProvider(Foo foo) { this.bar = foo.getBar(); }
public Injectable getInjectable(ComponentContext cc, Context c, Type
t) {
if (t.equals(Bar.class)) {
return this;
}
return null;
}
public ComponentScope getScope() { return ComponentScope.Singleton; }
public Bar getValue() { return bar; }
}
// This is how I'm *trying* to make Foo available to BarProvider
public class FooServletContainer extends ServletContainer {
private Foo foo;
public FooServletContainer(Foo foo) {
this.foo = foo;
}
protected void configure(WebConfig wc, ResourceConfig rc,
WebApplication wa) {
rc.getSingletons().add(foo);
rc.getProviderSingletons().add(new BarProvider(foo));
super.configure(wc, rc, wa);
}
}
Thanks in advance!
Best,
Kent