java - Is there a nice way create new Object with some parameters in its constructor and also to have CDI beans injected in it? -


example:  @dependant  public class somestartingpoint {  @inject private someservice someservice;     public void dosomething(long along, mycustomobject myobject) {        notabean notabean = new notabean(along, myobject);        someservice.dostuffwithnotabean(notabean);    } }   public class notabean {  @inject private wouldbeperfectifthiswouldbemanagedbean somehowinjectedbean;     public notabean(long along, mycustomobject myobject) {       //set state    } } 

so question is, there nice way have injected notabean object, supposed have state in it, created new()?

of course, in current situtation pass wouldbeperfectifthiswouldbemanagedbean argument constructor, not related question.

there's cdi 1.0 way , cdi 1.1 way this. 1.1 way easier 1.0, hence why created it.

here's example deltaspike: https://github.com/apache/deltaspike/blob/34b713b41cc1a237cb128ac24207b76a6bb81d0c/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/beanprovider.java#l437

    creationalcontext<t> creationalcontext = beanmanager.createcreationalcontext(null);      annotatedtype<t> annotatedtype = beanmanager.createannotatedtype((class<t>) instance.getclass());     injectiontarget<t> injectiontarget = beanmanager.createinjectiontarget(annotatedtype);     injectiontarget.inject(instance, creationalcontext); 

assuming have instance of object has fields or methods annotated @inject satisfy dependencies.

in cdi 1.1, can opposite. using class unmanaged can instantiate unmanaged instances of class. need call setters afterwards set values.

   unmanaged<foo> foou = new unmanaged(foo.class);    foo foo = foou.newinstance().get(); 

one other way, without using @inject use cdi 1.1 utility class manually references. instead of injecting reference someservice do:

   someservice someservice = cdi.current().select(someservice.class).get(); 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -