In this chapter we will expand on producers, which were introduced back in Chapter 1, What is a Bean?, by covering their uses in greater detail. We will also see what scopes our produced beans are in and how to ensure that the beans we produce don't leave pieces of themselves behind when they're destroyed.
We've already learned how producers can be useful when we want to make runtime decisions about which bean instance should be used in a given situation. But there can be many situations where producers are beneficial, such as enabling third-party frameworks to be used with CDI by exposing their objects as beans.
Producers allow us to utilize runtime polymorphism, such as:
@SessionScoped public class SearchManager implements java.io.Serializable { private SearchType searchType= SearchType.FICTION; @Produces @Preferred public BookSearch getSearch() { switch (searchType) { case FICTION: return new FictionSearch(); case NONFICTION: ...