Java Reference
In-Depth Information
In Listing 11‐6, you start with the code that disambiguates your Strings .
LISTING 11‐6: The annotation Qualii er interface
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
public @interface MessageEvent {
Type value();
The interface preceding class dei nes a MessageEvent qualii er and two enum types ( SERVICE and
PARAMETER ) that you will use to act as annotation to mark the strings to be i red by the event instances.
public class EventService {
private String message;
@Inject @MessageEvent(Type.SERVICE)
Event<String> serviceEvent;
@Inject @MessageEvent(Type.PARAMETER)
Event<String> parameterEvent;
public void startService(){"Starting service "+message);"‐d ‐p");
To use the Qualii ers , you just add the MyEvent annotation to the relevant injected instance with
the desired enum type in parenthesis. Then you later i re the events from within the startService
method, just as you did before in the previous example. The bold parts code lines are all you have
added to the previous example in the previous listing.
Now you'll add the annotations to the observer part. As you did before, you just have to add the
Qualii ers to the relevant @Observes annotation.
Search WWH ::

Custom Search