java - How to dynamically retrieve a CDI dependency? -
i'm trying dynamically retrieve dependency java code.
i know class name, want take instance managed container, 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 bar
instance myself because wouldn't able inject correct spam
. want if foo
had injected bar
it. can't add field @inject bar bar
because exact name of dependency varies in runtime.
any way this?
i'm using wildfly 8.2.0.
you can this:
public class foo { @inject beanmanager beanmanager; public void foo() { set<bean<?>> beans = beanmanager.getbeans(bar.class); bean<?> bean = beanmanager.resolve(beans); creationalcontext<?> creationalcontext = beanmanager.createcreationalcontext(bean); bar bar = (bar) beanmanager.getreference(bean, bar.class, creationalcontext); } }
or maybe simpler:
public class foo { @inject instance<object> instance; public void foo() { bar bar = instance.select(bar.class).get(); } }
Comments
Post a Comment