Coming back to our producer method at the start of the chapter, there is one problem that we may experience as the code is currently written. Creating FictionSearch
and NonFictionSearch
with the Java new
operator means that they are not eligible for dependency injection and cannot have interceptors.
Both of those restrictions may be acceptable to our application, but if they aren't then we can use dependency injection into the producer method to ensure that we are using beans controlled by Weld. The producer would now be as follows:
@Produces @Preferred @SessionScoped public BookSearch getSearch(FictionSearch fs, NonFictionSearch nfs) { switch (searchType) { case FICTION: return fs; case NONFICTION: return nfs; default: return null; } }
In our example, FictionSearch
and NonFictionSearch
are both @RequestScoped
beans; so what does that mean for our producer as we're producing a bean into @SessionScoped
? Our producer method has...