From the course: Java EE 8 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Producer methods

Producer methods - Java EE Tutorial

From the course: Java EE 8 Essential Training

Start my 1-month free trial

Producer methods

- [Instructor] CDI provides support for factory classes that grant developers greater control over the instantiation of beans. This support is accomplished using the Produces annotation, which allows us to create methods that act as a source of objects that can be injected by the CDI container. This maybe necessary in circumstances where a bean type needs to be determined dynamically at run time, or when an object requires special instantiation logic. Let's take a look at how to use the Produces annotation to handle these situations. To do that, we're going to create a new class within our JSF project, so we'll just open the new dialogue, select "Class," and then we're going to name this class, "The inventory service factory." So we're going to be using a factory to create an inventory service as opposed to allowing the CDI container just to instantiate our annotated classes. We'll go ahead and create the new class, and then…

Contents