When we want to specify qualifiers on an event we intend to fire, there are two ways it can be achieved:
As we've seen in previous examples, specifying qualifiers at an injection point is easy.
@Inject @Removed Event<Book> bookRemovedEvent;
Every call to bookRemovedEvent.fire()
will have the event qualifier @Removed
and would match the second observer method we defined earlier in the chapter.
The downside of specifying event qualifiers on the injection point, as just done, is that we are not able to specify event qualifiers dynamically. We can modify our previous Event
injection point to the following:
@Inject @Any Event<Book> bookRemovedEvent;
And instead set the qualifier dynamically by using this:
bookEvent .select( new AnnotationLiteral<Removed>(){} ) .fire(book);
The AnnotationLiteral
class is a helper class provided by CDI to make it easier...