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是类的标记接口:PushNotificationNew和PushNotificationFail。
听众:
@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。也许您知道更好的解决方案?
| 归档时间: |
|
| 查看次数: |
497 次 |
| 最近记录: |