When to use a CDI producer field or method
Join the DZone community and get the full member experience.Join For Free
Conforming to the official specification, a producer field can be used:
• instead of a simple getter method
• for declaring Java EE resources
Moreover, a producer method can be used – producer methods acts as sources of bean instances:
• to inject non-beans objects (expose any JDK class as a bean)
• to inject objects that have type that vary at runtime (vary the implementation of a bean type at runtime)
• to inject objects that requires some custom initialization that the bean constructor does not perform (define multiple beans, with different scopes or initialization, for the same implementation class).
Opinions expressed by DZone contributors are their own.