In this post under Spring Core, I will show with example how to create custom application events.
For our example, I created a custom event that the application will generate as shown below
CustomEvent
package package32;
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent {
public CustomEvent(Object source) {
super(source);
}
}
In the above code, I created a class named “CustomEvent” and it extends Spring’s “ApplicationEvent” class. Extending “ApplicationEvent” class is required to create a custom event class.
Now I will create a listener, that will listen to the above event and execute the code when the event is generated.
Listener
package package32;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class CustomEventListener {
@EventListener
public void customEventListener(CustomEvent customEvent) {
System.out.println("Custom Event Generated");
}
}
In the above code, we create a class “CustomEventListener” and added a method “customEventListener” method.
The method “customEventListener” takes “CustomEvent” instance as a parameter. The method is also annotated with “@EventListener” annotation. Annotating the method with “@EventListener” is important.
Below is the main class that shows how to publish the event.
Main class
1 package package32;
2
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.ApplicationEventPublisher;
5 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6 import org.springframework.context.annotation.ComponentScan;
7
8 @ComponentScan(basePackages = "package32")
9 public class Example32 {
10
11 public static void main(String[] args) {
12 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example32.class);
13 Example32 example32 = applicationContext.getBean(Example32.class);
14 example32.publishEvent(applicationContext);
15 }
16
17 public void publishEvent(ApplicationEventPublisher applicationEventPublisher) {
18 CustomEvent customEvent = new CustomEvent(this);
19 applicationEventPublisher.publishEvent(customEvent);
20 }
21 }
In the above class main method, I create an instance of “AnnotationConfigApplicationContext” named “applicationContext”
This instance “applicationContext” already implments the interface “ApplicationEventPublisher”.
At line 13, I get an instance of “Example32” using “getBean” method.
At line 14, I call “publishEvent” method passing the instance “applicationContext” as an argument.
In the method “publishEvent”, at line 18, I create an instance of “CustomEvent” class passing “Example32” instance as an constructor argument making it the source of the event.
At line 19, I call “publishEvent” method of “applicationEventPublisher” and pass the event instance as method argument.
In this way I publish the event and once published, the “CustomEventListener” instance will execute the method “customEventListener”.
In this way we can publish and listen to custom event.