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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -