@EventListener annotation can be used for listening to multiple events.
This can be done by specifying multiple classes as 'value' element of the annotation.
Examples
Using multiple event types
package com.logicbig.example;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.context.event.EventListener;
@Configuration
public class EventListenerMultipleEventTypesExample {
@Bean
AListenerBean listenerBean() {
return new AListenerBean();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(EventListenerMultipleEventTypesExample.class);
System.out.println("-- stopping context --");
context.stop();
System.out.println("-- starting context --");
context.start();
}
private static class AListenerBean {
@EventListener({ContextRefreshedEvent.class, ContextStoppedEvent.class,
ContextStartedEvent.class})
public void handleContextEvent() {
System.out.println("context event received ");
}
}
}
Outputcontext event received -- stopping context -- context event received -- starting context -- context event received
Using multiple event types and capturing event instance
In above example we didn't declare any parameter to our listener method handleContextEvent(). Following example shows that we can declare exactly one parameter which should be a super type of what we have specified in @EventListener#value()
package com.logicbig.example;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.context.event.EventListener;
@Configuration
public class EventListenerMultipleEventTypesExample2 {
@Bean
AListenerBean listenerBean () {
return new AListenerBean();
}
public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(EventListenerMultipleEventTypesExample2.class);
System.out.println("-- stopping context --");
context.stop();
System.out.println("-- starting context --");
context.start();
}
private static class AListenerBean {
@EventListener({ContextRefreshedEvent.class, ContextStoppedEvent.class,
ContextStartedEvent.class})
public void handleContextEvent (ApplicationEvent applicationEvent) {
System.out.println("context event received "+applicationEvent);
}
}
}
Outputcontext event received org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@8e8c9b0: startup date [Thu Jun 10 02:48:50 CDT 2021]; root of context hierarchy] -- stopping context -- context event received org.springframework.context.event.ContextStoppedEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@8e8c9b0: startup date [Thu Jun 10 02:48:50 CDT 2021]; root of context hierarchy] -- starting context -- context event received org.springframework.context.event.ContextStartedEvent[source=org.springframework.context.annotation.AnnotationConfigApplicationContext@8e8c9b0: startup date [Thu Jun 10 02:48:50 CDT 2021]; root of context hierarchy]
Example ProjectDependencies and Technologies Used: - spring-context 6.1.2 (Spring Context)
Version Compatibility: 4.2.0.RELEASE - 6.1.2 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 17
- Maven 3.8.1
|