To publish a custom event in Spring, define an event class (a record is enough), inject ApplicationEventPublisher, and call publishEvent(new YourEvent(...)). Any bean with an @EventListener method that accepts that type is called. Extending ApplicationEvent is optional since Spring Framework 4.2.
A custom event lets one part of your application announce a business fact, such as an invoice being issued, without depending on the code that reacts. This article focuses on designing the event type, publishing it from different places, and testing that it fires.
You have two options, and the second is the recommended one for new code.
| Approach | Example | When to use |
|---|---|---|
Extend ApplicationEvent | class InvoiceIssued extends ApplicationEvent | You need getSource() or getTimestamp(), or you maintain legacy code |
| Plain object or record | record InvoiceIssued(...) | Everything else |
A plain object is delivered to listeners wrapped in a PayloadApplicationEvent, but you never see the wrapper in a listener that takes the payload type directly.
The source argument must not be null; it is usually the object that publishes the event.
Records are immutable, which matters because several listeners may read the same instance. Keep events free of mutable entities: copy the identifiers and values listeners need, not a Hibernate entity that may be detached later.
Publish from a service method after the state change has happened.
If you cannot use constructor injection, implement ApplicationEventPublisherAware, which hands you the publisher through setApplicationEventPublisher. Constructor injection is simpler and is the standard choice.
Spring Data repositories can publish events for you. Annotate a method on an aggregate root with @DomainEvents and another with @AfterDomainEventPublication; the events are published when you call save(...).
This keeps publication inside the domain model and guarantees the event is sent only when the aggregate is saved.
For listeners that implement the older interface, ApplicationListener<InvoiceIssuedEvent> works the same way, but the annotation is shorter and supports conditions, ordering and return values.
Listeners run synchronously on the publisher's thread by default. Three settings change that.
@Async (with @EnableAsync) to a listener so it runs on an executor.@TransactionalEventListener so it runs only after the surrounding transaction commits.Spring Boot offers @RecordApplicationEvents, which captures events during a test.
The test asserts on the event itself, so it does not depend on any listener.
No. Any bean in the application context with an @EventListener method is registered automatically at startup.
Yes. All matching listeners are called, in @Order sequence if you specify one.
Yes. A listener parameter of a supertype receives every event assignable to it, which is useful for auditing.
Use @Async on the listener, or move the work to a message broker if it must survive restarts.
A custom Spring event is just a class, preferably an immutable record. Publish it with ApplicationEventPublisher.publishEvent, consume it with @EventListener, and choose @TransactionalEventListener or @Async when timing matters. Verify publication in tests with @RecordApplicationEvents.