java - CDI: Injecting resources to beans from external libraries -


in spring have annotation-based , xml-based configuration. while first recommended quick development, second more flexible , able handle special cases. there 2: injecting mocks junit tests , configuring beans external libraries.

i haven't found equivalent xml configuration cdi, question is, how handle dependency injection such beans? external libraries, need configured , there's no possibility add annotations them.

you have 3 solutions need:

use producer

cdi provides way transform non cdi class in beans. called producer. if want create bean class named noncdiclass have create

public class myproducers {     @produces     public noncdiclass producenoncdiclass() {         return new noncdiclass();         };     }   } 

you can @inject bean when needed.

you can put many producer method want in class.

if need simulate injection in produced bean can cdi injects parameters in producer methods calls.

@produces public noncdiclass producenoncdiclass(myfisrtbean param1, mysecondbean param2) {     noncdiclass res = new noncdiclass(param1);     res.setparam(param2);     return res;     }; } 

in example myfirstbean , mysecondbean existing bean classes injected cdi @ producing time.

producers can have qualifiers (on them or on parameters) or inject injectionpoint cdi internal bean allowing produce bean differently in function of injection , annotation has.

you have nice injectionpoint example in weld reference documentation.

develop extension

i won't go details here since don't know if it's need, can register bean in extension in afterbeanvalidation phase. these registered beans can of class want. should need more info develop here.

use seam solder (legacy) or wait deltaspike 0.6

solder integrated config module project no more maintained since it's been in process merged in apache deltaspike. merge in deltaspike roadmap version 0.6: http://issues.apache.org/jira/browse/deltaspike-271. start using solder config , switch deltaspike when it'll have feature (which should quite close). solution not favorite if want have config file à la spring, it's closest solution


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -