-
1. Re: When should one use createCreationalContext(null) and/or createCreationalContext(bean)?
manovotn Jul 22, 2019 4:17 AM (in response to ljnelson)Hi Laird,
your "mental model" is very close, CreationalContext is indeed a "holder" that glues together a normal scoped bean with its Dependent dependencies. That way you know when to destroy those Dependent beans.
Therefore, when operating with BM.getReference(...) you want to use the non-null variant so that dependent objects get destroyed properly.
For the method BM.createCreationalContext() itself, in CDI spec this is chapter 11.3.5. Obtaining a
CreationalContext
.What it says it that for any contextual instance you should be using the non-null variant. That means for any class that is backed by a CDI bean; in short whenever you can supply that, do it.
The other form with null is then used for non-contextual objects - e.g. whenever you want to create a CC for a class that doesn't have a bean backing it.
What I can think of is for instance when enforcing CDI injection into a non-CDI object via BM.createInjectionTarget(...).inject(foo, null).