Java Reference
In-Depth Information
This listing has a producer method that's responsible for returning a User bean repres-
enting the current user visiting the ActionBazaar website. This method is annotated with
the @Produces annotation . The @SessionScoped annotation informs the CDI
container that you want the instance stored in the current session. As a result, this method
will be invoked only once for each session instance; the instance will be cached in the ses-
sion. The @Named annotation makes the instance available to JSF, giving JSF a string
name that can be referenced from an EL expression. The next listing shows how the JSF
page might look when using this User bean.
Listing 12.7. Invoking the producer from index.xhtml
In this listing the producer method is invoked indirectly. Because the producer method is
generating an instance which will be cached in the session, you want to invoke the produ-
cer method only when the user has authenticated and you have a principal object with the
role of seller or bidder. So you first check to see if the user is authenticated . If the user
is authenticated , then you evaluate the expression to display the username that results
in either the current user instance being retrieved from the session object or the producer
method invoked with the resulting value being cached in the session. If the visitor hasn't
authenticated yet, you render a link to authenticate
.
With this code example, it's important to remember that CDI will scan all of the classes in
a JAR file—the JAR file containing a beans.xml file. It'll keep track of all producer meth-
ods it discovers while analyzing the classes in the JAR file. When an instance of the bean
is requested—a user as in the case of this example—CDI will invoke the producer method
if an instance of the bean doesn't already exist in the current scope.
Search WWH ::




Custom Search