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.