泛型类型的Spring事件路由

问题描述

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

事件类:

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;
    }
}

此处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);
}

问题:PushNotificationStoreEvent<PushNotificationNew>PushNotificationStoreEvent<PushNotificationFail> 对于侦听器没有区别。

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

我可以通过在每种情况下创建单独的事件类或使用一个侦听器并通过instanceofevent.push选择正确的操作来解决此问题。也许您知道更好的解决方案?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)