I'm trying to dynamically retrieve a dependency for my Java code.
I know the class name, but I want to take the instance managed by the container, with proper indirect dependencies resolved.
For example:
class Foo {
public static void foo() {
Bar bar = (Bar) getDependency("com.example.Bar");
bar.bar();
}
}
class Bar {
@Inject
private Spam spam;
public void bar() {
spam.spam();
}
}
I can't construct a Bar
instance myself because I wouldn't be able to inject the correct Spam
. So I want as if Foo
had injected a Bar
into it. I can't add a field like @Inject Bar bar
because the exact name of the dependency varies in runtime.
Any way to do this?
I'm using WildFly 8.2.0.