Java Reference
In-Depth Information
@Produces [2]
@Named
public Collection<Seat> getSeats() {
return Lists.newArrayList(seats);
}
public void
onMemberListChanged(@Observes(notifyObserver =
Reception.IF_EXISTS) final Seat member) {
retrieveAllSeatsOrderedByName(); [3]
}
}
At first, have a look at the
@Model
annotation
[1]
, which is an alias (we call this kind of
annotations
stereotypes
) for two commonly used annotations:
@Named
and
@RequestScoped
. Therefore, this bean will be named into our JSF page and will carry
a request scope.
Next, pay attention to the
getSeats
method. This method returns a list of seats, expos-
ing it as a
producer
method
[2]
.
Note
The
producer
method allows you to have control over the production of the dependen-
cy objects. As a Java factory pattern, they can be used as a source of objects whose imple-
mentation may vary at runtime or if the object requires some custom initialization that is
not to be performed in the constructor.
It can be used to provide any kind of concrete class implementation; however, it is espe-
cially useful to inject Java EE resources into your application.
One advantage of using a
@Producer
annotation for the
getSeats
method is that its
objects can be exposed directly via JSF's
Expression Language
(
EL
), as we will see in a
minute.
Finally, another feature of CDI that was unleashed in this example is the
observer
. An ob-
server, as the name suggests, can be used to observe events. An observer method is noti-
fied whenever an object is created, removed, or updated. In our example, it allows the list
of seats to be refreshed whenever they are needed.