Correct way to programatically instantiate a Bean

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Correct way to programatically instantiate a Bean

rinswind
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
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Correct way to programatically instantiate a Bean

Matej Novotny
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" <[hidden email]>
> To: [hidden email]
> 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
> [hidden email]
> 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.
_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Fwd: Correct way to programatically instantiate a Bean

rinswind
Including CDI-DEV

---------- Forwarded message ----------
From: Todor Boev <[hidden email]>
Date: Fri, Sep 8, 2017 at 11:03 AM
Subject: Re: [cdi-dev] Correct way to programatically instantiate a Bean
To: Matej Novotny <[hidden email]>


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 <[hidden email]> 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" <[hidden email]>
> To: [hidden email]
> 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
> [hidden email]
> 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.



_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Correct way to programatically instantiate a Bean

Matej Novotny
In reply to this post by Matej Novotny
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" <[hidden email]>
> To: "Matej Novotny" <[hidden email]>
> 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 <[hidden email]> 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" <[hidden email]>
> > > To: [hidden email]
> > > 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
> > > [hidden email]
> > > 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.
> >
>
_______________________________________________
cdi-dev mailing list
[hidden email]
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.