是否可以使Spring ApplicationListener监听2种或更多类型的事件?

Luk*_*uke 25 java events spring

我有两种不同类型的事件,我希望我的班级能够相应地(并且不同地)收听和处理.

我试过了: public class ListenerClass implements ApplicationListener<Foo>, ApplicationListener<Bar>

这给了我一个错误,你不能用不同的参数两次实现相同的接口.

如果没有实现ApplicationEvent的监听器(或Foo和Bar将实现的一些其他通用接口)并使用它instanceof来确定要采取的路径,我还有其他选择吗?

谢谢!

Ral*_*lph 35

请参阅本答案末尾的Spring 4.2更新!

春天<4.2

并不是的.

您可以为参数使用公共超类(例如ApplicationEvent)或Foo和Bar实现的公共接口,然后您必须自己使用它.

public class ListenerClass implements ApplicationListener<ApplicationEvent> {
    ...
    if(event instanceOf Foo || event instance of Bar) {
    }
}
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用两个应用程序监听器

public class ListenerClass {

    void onFoo(Foo foo){}
    void onBar(Bar bar){}

    static class FooListener implements ApplicationListener<Foo> {
       ListenerClass listerner;
       ....
       public void onApplicationEvent(Foo foo) {
           listener.onFoo(foo);
       }
    }
    static class BarListener implements ApplicationListener<Bar> {
       ListenerClass listerner;
       ....
       public void onApplicationEvent(Bar bar) {
           listener.onBar(bar);
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

重要提示:所有3个实例都必须是spring beans!


当然,您可以自己实现这些功能.您至少有两个不同的选择,基于spring事件调度程序框架进行创建或完全分离.对于第二个选择,看看CDI-Event Mechanim,可能会搜索一些弹簧端口.

几年前我已经实现了自己的第一选择(我想在2007/2008).我负责监督所有事件的事件调度员.它是通过XML文件配置的.这个xml文件包含"引用"!对于应该调度的每个事件,bean中的方法 - 这些方法将由反射调用.因此,可以使用强类型事件处理程序方法(这是该方法的目标),但也可以在一个类中使用多个处理程序方法.现在我会跳过xml文件并使用Annotations和Bean-Post-Processor


Spring 4.2更新

Spring 4.2将有一个改进的事件监听器配置(基于注释),这使得在一个bean中有两个不同的事件监听器方法成为可能.

@Component
public class ListenerClass {

  @EventListener
  public void handleFooEvent(Foo fooEvent) {...}

  @EventListener
  public void handleBarEvent(Bar barEvent) {...}

}
Run Code Online (Sandbox Code Playgroud)