The role of the listener
- Decoupling: Through the listener, you can transfer
Event sender
andEvent handler
Decoupling reduces the dependence between the two. - Event driver: The listener allows the program to
Event-driven
Run in the form of that is, when a specific event occurs, the corresponding processing logic will be automatically triggered. - Cross-component communication: Communication and collaboration can be achieved through listeners between different components or modules.
In Spring Boot, listeners can be created by implementing the listener interface or annotations provided by Spring. Spring provides a variety of types of listeners, including ApplicationListener for listening for application events, @EventListener annotation for listening for specific events, etc.
ApplicationEvent is a core class in the Spring framework for publishing and listening to events in applications. It is the base class for all Spring events, an abstract class that can be inherited to create custom events. Events can be passed and communicated between different components in an application.
ApplicationEvent carries an Object object that can be published. After the event listener listens to this event, custom logic will be triggered (operating with the Object object). It is an important mechanism for implementing event-driven programming. Through the collaboration of events and listeners, decoupling and efficient communication between different components can be achieved.
Implement ApplicationListener to listen for events
1. CreateCustomEvent
Event category
import ; public class CustomEvent extends ApplicationEvent { // CustomEvent needs to be inherited from ApplicationEvent private String message; public CustomEvent(Object source, String message) { super(source); = message; } public String getMessage() { return message; } }
2. Custom listener, listen to event classes
import ; import ; import ; @Component public class CustomEventListener implements ApplicationListener<CustomEvent> { @Override public void onApplicationEvent(CustomEvent event) { // When the CustomEvent event is published, this method will be called ("Surveyed event success - " + ()); // Write the logic for handling events here // For example, compliant messages } }
3. Trigger the listening event
import ; import ; import ; @Component public class EventPublisher { private final ApplicationContext applicationContext; @Autowired public EventPublisher(ApplicationContext applicationContext) { = applicationContext; } public void publishCustomEvent(String message) { // Publish CustomEvent CustomEvent customEvent = new CustomEvent(this, message); (customEvent); } }
When the publishCustomEvent method is called and CustomEvent is published, all listeners that implement ApplicationListener<CustomEvent> will be notified and execute the onApplicationEvent method.
Listen to events using @EventListener annotation
If you want to use annotations to create listeners, you can use @EventListener annotation:
import ; import ; @Component public class AnnotationBasedEventListener { @EventListener public void handleCustomEvent(CustomEvent event) { // When the CustomEvent event is published, this method will be called ("Surveyed event success - " + ()); // Write the logic for handling events here // For example, compliant messages } }
use@EventListener
Annotations can simplify the process of creating listeners and can be found inAt the method level
Specify the type of event to be listened to.
Whether through implementation of interfaces or using annotations, Spring Boot provides event-driven application logic.
This is the end of this article about SpringBoot's method examples of creating listeners. For more related content on SpringBoot, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!