泛型类型的 Spring 事件路由

Wol*_*and 5 java spring spring-context

我想基于事件通用类型构建事件处理。

活动类别:

public class PushNotificationStoreEvent<T extends Push> extends ApplicationEvent {
    private static final long serialVersionUID = -3791059956099310853L;

    public T push;

    public PushNotificationStoreEvent(Object source, T push) {
        super(source);
        this.push = push;
    }
}
Run Code Online (Sandbox Code Playgroud)

Push是类的标记接口:PushNotificationNewPushNotificationFail

听众:

@Async
@EventListener
public void storeNewPush(PushNotificationStoreEvent<PushNotificationNew> event) {
    pushNewRepoService.save(event.push);
}
@Async
@EventListener
public void storeFailPush(PushNotificationStoreEvent<PushNotificationFail> event) {
    pushFailRepoService.save(event.push);
}
Run Code Online (Sandbox Code Playgroud)

PushNotificationStoreEvent<PushNotificationNew>问题:听众之间和听众之间没有区别PushNotificationStoreEvent<PushNotificationFail>

这意味着所有事件PushNotificationStoreEvent都由所有侦听器处理。

我可以通过为每个案例创建单独的事件类或使用一个监听器并选择正确的操作来解决这个instanceof问题event.push。也许您知道更好的解决方案?