Spring event publishing and listening¶
Spring event publishing and listening is a mechanism provided by the Spring Framework that allows for loose coupling between components through an observer pattern implementation. It enables communication between beans where the publisher does not need to know who the subscribers are.^[600-developer__java__spring__spring-note.md]
Core Interfaces¶
The mechanism relies on two primary interfaces:
ApplicationEventPublisher: Used to publish events.- It defines the
publishEvent(Object event)method. - If the published event is not an
ApplicationEvent, it is automatically wrapped in aPayloadApplicationEvent.^[600-developer__java__spring__spring-note.md]
- It defines the
ApplicationListener<E extends ApplicationEvent>: Used to listen for events.- It defines the
onApplicationEvent(E event)method, which handles the event.^[600-developer__java__spring__spring-note.md]
- It defines the
Related Concepts¶
- [[Spring]]
- [[Observer pattern]]
- [[BeanFactory]]
Sources¶
^[600-developer__java__spring__spring-note.md]