Skip to content

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 a PayloadApplicationEvent.^[600-developer__java__spring__spring-note.md]
  • 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]
  • [[Spring]]
  • [[Observer pattern]]
  • [[BeanFactory]]

Sources

^[600-developer__java__spring__spring-note.md]