Spring Event System¶
The Spring Event System provides a mechanism for communication and decoupling between components within the Spring container. It is built upon the standard Observer design pattern, allowing components to publish events and consume them without needing to know about each other directly^[600-developer-java-spring-spring-note.md].
Core Interfaces¶
The system relies on two primary interfaces: the publisher and the listener.
ApplicationEventPublisher¶
Components that need to broadcast events use the ApplicationEventPublisher interface.^[600-developer-java-spring-spring-note.md]
This interface defines a single method, publishEvent(Object event), which notifies all matching listeners registered with the application.^[600-developer-java-spring-spring-note.md]
* If the published object is not an instance of ApplicationEvent, the framework automatically wraps it in a PayloadApplicationEvent.^[600-developer-java-spring-spring-note.md]
ApplicationListener¶
Components that react to events must implement the ApplicationListener<E extends ApplicationEvent> interface.^[600-developer-java-spring-spring-note.md]
* This interface is a FunctionalInterface with the method onApplicationEvent(E event).^[600-developer-java-spring-spring-note.md]
* The generic type <E> specifies the exact type of event the listener is interested in.
Related Concepts¶
- [[Observer Pattern]]
- Spring Framework
Sources¶
^[600-developer-java-spring-spring-note.md]