在Spring中的多个事件上使用@EventListener批注 [英] Use @EventListener annotation on multiple events in Spring

查看:941
本文介绍了在Spring中的多个事件上使用@EventListener批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个非常简单的问题,但我似乎在任何地方都找不到答案.

This seems like a pretty straight forward question, but I can't seem to find the answer anywhere.

在Spring中,我可以使用@EventListener批注创建事件的侦听器,如下所示:

In Spring, I can create a listener for an event using the @EventListener annotation, like this:

@Component
public class MyListener {

    @EventListener
    public void handleEvent(ContextRefreshedEvent event) {
        ...
    }
}

但是,如果我需要相同的方法来侦听多个事件并根据发生的事件采取不同的行动怎么办?

However, what if I need the same method to listen to multiple events and act differently based the event that happens?

直觉上,我在想类似的东西:

Intuitively, I'm thinking something similar to this:

    @Component
    public class MyListener {

        @EventListener
        public void handleEvents(ContextRefreshedEvent event, ContextStopped event) {
             String event;
             if(event instanceof ContextRefreshedEvent)
                  event = "Refreshed";
             if(event instanceof ContextStoppedEvent)
                  event = "Stopped";
        }
    }

EventListener批注侦听多个事件的正确方法是什么?同一方法如何根据发生的实际事件来区分?

What is the correct way for the EventListener annotation to listen to multiple events and how can the same method differentiate based on the actual event that happens?

非常感谢.

推荐答案

创建一个侦听多个事件的事件侦听器在名义上很容易:

It's nominally easy to create an event listener that listens for multiple events:

@EventListener({EventA.class, EventB.class})
public doSomething() {
   ...
}

但是很明显,这种方法不能让您访问基础事件.根据EventListener的javadoc,似乎无法执行您的建议

But obviously this approach does not give you access to the underlying event. Based on the javadoc for EventListener it does not appear to be possible to do what you are suggesting

如果带注释的方法支持单个事件类型,则该方法可能 声明一个单个参数,该参数反映要监听的事件类型. 如果带注释的方法支持多种事件类型,则此注释 可以使用这些类来引用一种或多种受支持的事件类型 属性.有关更多详细信息,请参见classes()javadoc.

If an annotated method supports a single event type, the method may declare a single parameter that reflects the event type to listen to. If an annotated method supports multiple event types, this annotation may refer to one or more supported event types using the classes attribute. See the classes() javadoc for further details.

...

如果( classes)属性用单个值指定,带注释 方法可以选择接受单个参数.但是,如果这 属性具有多个值,带注释的方法必须 不声明任何参数.

If (the classes) attribute is specified with a single value, the annotated method may optionally accept a single parameter. However, if this attribute is specified with multiple values, the annotated method must not declare any parameters.

因此,似乎没有任何机制可以消耗多个事件并根据这些事件的主体采取不同的操作.我建议不要这样做,您可以总是注册特定于事件的@EventListener方法,然后让它们调用共享方法来执行任何常用功能.

Thus there does not appear to be any mechanism to consume multiple events and take a different action based on the body of those events. I would suggest that this shouldn't be necessary though, you could always register event-specific @EventListener methods, and then just have them call a shared method to perform any common functionality.

来源: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/event/EventListener.html

这篇关于在Spring中的多个事件上使用@EventListener批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆