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