[cdi-dev] Correct way to programatically instantiate a Bean

Matej Novotny manovotn at redhat.com
Fri Sep 8 04:35:37 EDT 2017


Hi,

sorry for the ambiguity of the answer.
I meant contexual instance - e.g. each individual object of type T (aka your first code snippet).

BTW keep hitting "reply to all" so that we keep cdi-dev list in the loop :)

Matej

----- Original Message -----
> From: "Todor Boev" <rinsvind at gmail.com>
> To: "Matej Novotny" <manovotn at redhat.com>
> Sent: Friday, September 8, 2017 10:03:21 AM
> Subject: Re: [cdi-dev] Correct way to programatically instantiate a Bean
> 
> Hi Matej,
> 
> I am struggling to understand what do you mean by "bean".
> The Bean<T> object that produces objects of type T, or each individual
> object of type T?
> 
> If it is the T this should be the first of my code snippets: a new
> CreationalContext for every new T.
> If it is the Bean<T> this should be the second of my code snippets: one
> CreationalContext all new T.
> 
> Regards,
> Todor
> 
> On Fri, Sep 8, 2017 at 8:42 AM, Matej Novotny <manovotn at redhat.com> wrote:
> 
> > Hi Todor,
> >
> > if I am not mistaken, the CreationalContext is a way to bind
> > dependent instances to bean (and so align their lifecycles).
> >
> > Which means I'd use new CreationalContext for each bean.
> >
> > Matej
> >
> > ----- Original Message -----
> > > From: "Todor Boev" <rinsvind at gmail.com>
> > > To: cdi-dev at lists.jboss.org
> > > Sent: Thursday, September 7, 2017 5:07:35 PM
> > > Subject: [cdi-dev] Correct way to programatically instantiate a Bean
> > >
> > > Hello,
> > >
> > > I need to create beans from a portable extension.
> > > It is not clear to me if every instance of a bean must have an associated
> > > instance of CreationalContext or rather one CreationalContext must be
> > used
> > > for all instances:
> > >
> > > // Store a CreationalContext per bean instance
> > > BeanManager manager = ...
> > > Bean<T> bean = ...
> > > Context ctx = manager.getContext(bean.getScope());
> > >
> > > Map<T, CreationalContext<T>> dependents = ...
> > >
> > > public T createBean() {
> > > CreationalContext<T> cctx = manager.createCreationalContext(bean);
> > > T instance = ctx.get(bean, cctx);
> > > dependents.put(instance, cctx);
> > > return instance;
> > > }
> > >
> > > public void destroyBean(T instance) {
> > > dependents.computeIfPresent(instance, (inst, cctx) -> {
> > > bean.destroy(inst, cctx);
> > > return null;
> > > });
> > > }
> > >
> > > // CDI tracks dependents for me
> > > BeanManager manager = ...
> > > Bean<T> bean = ...
> > > Context ctx = manager.getContext(bean.getScope());
> > > CreationalContext cctx = manager.createCreationalContext(bean);
> > >
> > > public T createBean() {
> > > T instance = ctx.get(bean, cctx);
> > > return instance;
> > > }
> > >
> > > public void destroyBean(T instance) {
> > > bean.destroy(instance, cctx);
> > > }
> > >
> > > Best Regards,
> > > Todor
> > >
> > > _______________________________________________
> > > cdi-dev mailing list
> > > cdi-dev at lists.jboss.org
> > > https://lists.jboss.org/mailman/listinfo/cdi-dev
> > >
> > > Note that for all code provided on this list, the provider licenses the
> > code
> > > under the Apache License, Version 2
> > > (http://www.apache.org/licenses/LICENSE-2.0.html). For all other ideas
> > > provided on this list, the provider waives all patent and other
> > intellectual
> > > property rights inherent in such information.
> >
> 


More information about the cdi-dev mailing list