java - Pass runtime arguments to Object containing Injections -


context:

based on xml java ee application receives, want make new areeconfiguration object in injection of 3 objects happens. selection of correct instance<> happens based on information in xml. therefore, passing around information xml.

i have datastructure should filled these areeconfiguration objects @ runtime:

private hashmap<integer, areeconfiguration> configurations = new hashmap<integer, areeconfiguration>();  public void parsenewconfiguration(element el) throws invaliddescriptorexception {     if(!isvalidconfiguration(el)) throw new invaliddescriptorexception();      int key = getuniquekey();      areeconfiguration cfg = new areeconfiguration(key, el);            configurations.put(key, cfg); } 

my areeconfiguration object should (ideally) constructed essential xml information , inject objects. later, want use information xml pick right instance<>.

public class areeconfiguration {  @inject private instance<areeinput> ais; private areeinput ai;  @inject private instance<areereasoner> ars; private areereasoner ar;  @inject private instance<areeoutput> aos; private areeoutput ao;  areeconfiguration(int key, element el) throws invaliddescriptorexception {     ... }     @postconstruct public void choosecomponents(){          ai = chooseinput();     ar = choosereasoner();     ao = chooseoutput(); } 

what have found , tried:

through research (here @ stack overflow), understand cdi not inject in objects have been created new object(). code displayed above, never enters choosecomponents().

when want try using @producer, annotate parsenewconfiguration(element el) @producer , add @new areeconfiguration cfg argument. however, cannot pass element el too, unfortunate because contains essential xml information.

please ask additional questions if did not explain myself thoroughly. looking way accomplish above using cdi.

additionally, how 'clean' solution proposed in answer of question: @inject working pojos created cdi container?

to perform injection instances not created cdi container, , have access beanmanager, can like:

// create creational context beanmanager creationalcontext creationalcontext = beanmanager.createcreationalcontext(null);  // create injection target type of instance need inject injectiontarget injectiontarget = beanmanager.createinjectiontarget(beanmanager.createannotatedtype(instance.getclass()));  // perform injection instance injectiontarget.inject(instance, creationalcontext);  // call postconstruct on instance injectiontarget.postconstruct(instance); 

an example of class can used work at: http://seamframework.org/documentation/howdoidononcontextualinjectionforathirdpartyframework.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -