Including CDI-DEV
---------- Forwarded message ----------
From: Todor Boev <rinsvind(a)gmail.com>
Date: Fri, Sep 8, 2017 at 11:03 AM
Subject: Re: [cdi-dev] Correct way to programatically instantiate a Bean
To: Matej Novotny <manovotn(a)redhat.com>
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(a)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(a)gmail.com>
> To: cdi-dev(a)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(a)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.