Java Reference
In-Depth Information
With this annotation, you replace @Named and @RequestScoped with @Model , as
shown in the following snippet:
@Model
public class ItemController implements Serializable {
...
}
Stereotypes make CDI beans much easier to read. In the next section we'll take a look at
CDI's support for events.
12.6. Injecting events
Dependency injection is extremely useful for acquiring references to other beans. But CDI
has a slight twist on dependency injection with support for injecting events. This enables
events to be delivered to other beans without any compile-time dependencies. The genius
of this approach is that you can use an annotation to mark a method as being a recipient of
the event and you're finished. The event will be delivered to your bean without any further
work. You don't need to implement an interface or register your class as a listener to re-
ceive events. The container takes care of everything. Raising an event is similarly as easy;
the object responsible for dispatching events is also injected into the class that needs to
send the event.
The @Observes annotation is placed on one or more parameters of the method that's to
receive an event. The type of the parameter is the type of the event. For example, if the type
of the event were an ActionEvent , then the method would receive all ActionEvent s
that are fired. Attaching qualifiers to the method injection points provides additional spe-
cificity beyond just the type. Additional parameters are treated as beans that the container
will either look up or instantiate.
To fire an event, the bean that will send the event uses an Event object that's typed for
the object that's to be fired. It's injected using the @Inject annotation and can optionally
possess qualifier annotations that will narrow the list of potential targets for the event. The
Event object has a fire method that's used to send an event.
To better understand how events work in CDI, turn your attention to an example from Ac-
tionBazaar. In ActionBazaar, when a new item is listed, multiple beans need to be notified.
Search WWH ::




Custom Search