This seems like a pretty straight forward question, but I can't seem to find the answer anywhere.
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";
}
}
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?
Unfortunately, you can't pass in multiple events to a single listener like you might in jQuery and other frameworks. For example, you cannot do this: document. addEventListener('click mouseover', function (event) { // do something... }, false);
If we return a non-null value from a method annotated with @EventListener as the result, Spring Framework will send that result as a new event for us. Moreover, we can publish multiple new events by returning them in a collection as the result of event processing.
Spring boot event listener annotation @EventListener is used to create a method that listens for spring boot events. A spring boot event is published using the ApplicationEventPublisher class. The @EventListener annotated methods are invoked when an event is published using the ApplicationEventPublisher class.
ContextRefreshedEvent. This event is published when the ApplicationContext is either initialized or refreshed. This can also be raised using the refresh() method on the ConfigurableApplicationContext interface.
Why don't you do something like this.
From SpringFramework 4.2
, you can publish events without extending ApplicationEvent
.
As you replied you want to mostly listen to custom events. You can do something like this.
Create a Base class called BaseEvent
.
public class BaseEvent {
private String type;
public BaseEvent() {}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Consider that as the base class whenever you want to publish a custom Event
.
Now lets create two custom events called Example1Event
and Example2Event
.
Example1Event
Class
public class Example1Event extends BaseEvent {
private String message;
public Example1Event() {}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Example2Event
Class
public class Example2Event extends BaseEvent {
private String message;
public Example2Event() {}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Your EventListener
would look like this.
public class EventReceiver {
public EventReceiver() {}
@EventListener
public void receiveEvent(BaseEvent event) {
String eventType = event.getType();
if (eventType.equals("example1")) {
Example1Event example1Event = (Example1Event) event;
System.out.println(example1Event.getMessage());
} else if (eventType.equals("example2")) {
Example2Event example2Event = (Example2Event) event;
System.out.println(example2Event.getMessage());
}
}
}
This will work for what you want to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With