In Spring, when I write an EventHandler, I can set a conditon, to filter out uninteresting events, like this:
// I use lombok
public class TopicEvent extends ApplicationEvent {
@Getter @Setter private String topic;
@Getter @Setter private PayloadObject payload;
}
...
@EventListener(condition = "#event.topic eq \"ecology\"")
public void onEcologyTopicEvent(TopicEvent e) {
...
}
Which is already nice. But it has little benefit over
@EventListener
public void onEcologyTopicEvent(TopicEvent e) {
if (!e.getTopic().equals("ecology") { return; }
...
}
What I'd like to provide to the users of my TopicEvent is an Annotation
@TopicEventListener(topic = "ecology")
public void onEcologyTopicEvent(TopicEvent e) {
...
}
I have three ideas for that:
1: Spring offers thos synthesized Annotations and @AliasFor. Maybe it is possible to use that
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@EventListener
public @interface TopicEventListner {
@AliasFor(annotation = EventListener.class, /* can I tweak topic to the string #event.topic eq $topic? */)
String topic;
}
2: (What seems more plausible) Can I register some infrastructure components, maybe a custom ApplicationEventMulticaster
or add filters to EventListeners on runtime? If so, where would be a good place to start, i.e. which would be the class/component I would need to implement to register it where?, respectively - where could I hook into?
3: Replace @TopicEventListener(topic = "ecology")
by @EventListener(condition = "#event.topic eq \"ecology\"")
on compile time. But this approach seems to be... maybe a slight overkill, and I have not the slightest clue about such things, and expect it to be terribly complex.
... But it might be the way I would solve it in C++ (with a macro)