From the course: Java EE: Contexts and Dependency Injection

Unlock the full course today

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

What are producer methods?

What are producer methods?

From the course: Java EE: Contexts and Dependency Injection

Start my 1-month free trial

What are producer methods?

- [Narrator] Producer methods overcome inherent issues with letting the container manage the instantiation of beans. They allow any class to become a CDI managed bean, and therefore injectable. This includes classes that we have created programmatically, classes that require some custom initialization, types whose implementation might change at runtime, and, interestingly, those classes that we don't have control over, such as java core classes, and classes from compiled third party APIs. For example, we can use a producer method to produce a list of random numbers and inject it into any class. The array list that is produced here is injected in the same way that you would inject a CDI bean, with the inject annotation. Producer methods are often used to make instances of third party classes injectable. A typical example is to make a third party look at injectable so that the new instance does not have to be manually created each time. All that is required is that the producer method…

Contents