|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT | |||||||||
@Retention(value=RUNTIME) @Target(value=METHOD) public @interface EventSubscriber
An Annotation for subscribing to EventService Events.
This annotation simplifies much of the repetitive boilerplate used for subscribing to EventService Events.
Instead of this:
public class MyAppController implements EventSubscriber {
public MyAppController {
EventBus.subscribe(AppClosingEvent.class, this);
}
public void onEvent(EventServiceEvent event) {
AppClosingEvent appClosingEvent = (AppClosingEvent)event;
//do something
}
}
You can do this:
public class MyAppController { //no interface necessary
public MyAppController {
AnnotationProcessor.process(this);//if not using AOP
}
@EventSubscriber
public void onAppClosingEvent(AppClosingEvent appClosingEvent) {//Use your own method name with typesafety
//do something
}
}
That's pretty good, but when the controller does more, annotations are even nicer.
public class MyAppController implements EventSubscriber {
public MyAppController {
EventBus.subscribe(AppStartingEvent.class, this);
EventBus.subscribe(AppClosingEvent.class, this);
}
public void onEvent(EventServiceEvent event) {
//wicked bad pattern, but we have to this
//...or create multiple subscriber classes and hold instances of them fields, which is even more verbose...
if (event instanceof AppStartingEvent) {
onAppStartingEvent((AppStartingEvent)event);
} else (event instanceof AppClosingEvent) {
onAppStartingEvent((AppClosingEvent)event);
}
}
public void onAppStartingEvent(AppStartingEvent appStartingEvent) {
//do something
}
public void onAppClosingEvent(AppClosingEvent appClosingEvent) {
//do something
}
}
You can do this:
public class MyAppController {
public MyAppController {
AnnotationProcessor.process(this);//this line can be avoided with a compile-time tool or an Aspect
}
@EventSubscriber(eventClass=AppStartingEvent.class)
public void onAppStartingEvent(AppStartingEvent appStartingEvent) {
//do something
}
@EventSubscriber(eventClass=AppAppClosingEvent.class)
public void onAppClosingEvent(AppClosingEvent appClosingEvent) {
//do something
}
}
Brief, clear, and easy.
| Optional Element Summary | |
|---|---|
java.lang.Class<? extends EventService> |
autoCreateEventServiceClass
Whether or not to autocreate the event service if it doesn't exist on subscription, default is true. |
java.lang.Class |
eventClass
The class to subscribe to, if not specified, a subscription is created for the type of the method parameter. |
java.lang.String |
eventServiceName
The event service to subscribe to, default to the EventServiceLocator.SERVICE_NAME_EVENT_BUS. |
boolean |
exact
Whether or not to subscribe to the exact class or a class hierarchy, defaults to class hierarchy (false). |
int |
priority
Determines the order in which this subscriber is called, default is FIFO. |
ReferenceStrength |
referenceStrength
Whether to subscribe weakly or strongly. |
public abstract java.lang.Class eventClass
public abstract int priority
public abstract boolean exact
public abstract ReferenceStrength referenceStrength
public abstract java.lang.String eventServiceName
public abstract java.lang.Class<? extends EventService> autoCreateEventServiceClass
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT | |||||||||