Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

How do you publish an application event in Spring today, and do you still need to extend ApplicationEvent? Show a minimal publisher and the type the listener receives.

We inject ApplicationEventPublisher and call publishEvent(event). Since Spring 4.2 the event can be any object, so we no longer need to extend ApplicationEvent; Spring wraps a plain object in a PayloadApplicationEvent, and a listener can simply declare the payload type, as in @EventListener void on(OrderPlaced e). By default the listeners run synchronously on the publishing thread.

Why this question is asked

Application events are Spring's built-in way for one part of an application to announce that something happened without knowing who cares. They are often the first step towards decoupling modules, so interviewers ask how to publish one to check that we know the modern style and not only the old ApplicationEvent subclass from older tutorials. In this answer, we will recap what an application event is, write a minimal publisher and listener, see what the listener actually receives and why, and look at what happens on the publishing thread.

1A Quick Recap of Application Events

An application event is an object that describes something that happened, such as an order being placed. A publisher hands it to the application context, and the context passes it to every listener interested in that type. The publisher does not know which listeners exist, so we can add an email sender or an analytics recorder without changing the order code.

Before Spring 4.2, an event had to be a subclass of ApplicationEvent, which carries a source object and a timestamp, and a listener had to implement ApplicationListener<E>. Both still work, but they put Spring types into our domain classes. Since 4.2, any object can be published.

2A Minimal Publisher and Listener

The event is a plain record. The publisher injects ApplicationEventPublisher, an interface that the application context implements, and the listener is a method annotated with @EventListener.

Java
public record OrderPlaced(long orderId, BigDecimal total) {}
@Service
class OrderService {
private final ApplicationEventPublisher events;
OrderService(ApplicationEventPublisher events) { this.events = events; }
public void place(long id, BigDecimal total) {
// ... save the order ...
events.publishEvent(new OrderPlaced(id, total));
log.info("published");
}
}
@Component
class ConfirmationEmail {
@EventListener
void on(OrderPlaced e) {
log.info("sending email for order {}", e.orderId());
}
}
// orderService.place(42, new BigDecimal("19.99")) logs:
// sending email for order 42
// published

Notice the order of the two log lines. The listener ran before publishEvent() returned, so "published" comes second. We come back to why in a moment.

We could also inject ApplicationContext and call publishEvent() on it, but ApplicationEventPublisher is the narrower interface, so it states what the class needs and is easier to replace in a unit test.

3What the Listener Actually Receives

Internally, Spring's event system still works with ApplicationEvent objects. So when we publish a record that is not an ApplicationEvent, the context wraps it in a PayloadApplicationEvent<OrderPlaced>, which holds our record as its payload and also records the payload's type.

When Spring matches listeners, it looks at the method's parameter. If the parameter is OrderPlaced, Spring unwraps the payload and passes our record directly. If we really want the wrapper, for example to read the timestamp, we can declare the parameter as PayloadApplicationEvent<OrderPlaced> instead.

Java
@EventListener
void raw(PayloadApplicationEvent<OrderPlaced> e) {
log.info("{} at {}", e.getPayload().orderId(), e.getTimestamp()); // 42 at 1762300000000
}
Step throughPublishing a plain record
OrderServiceContextMulticasterConfirmationEmail
publishEvent(OrderPlaced)
wrap in PayloadApplicationEvent
multicastEvent(event)
on(OrderPlaced): payload unwrapped
returns
publishEvent returns

6 messages in order. Play them, or step through them one at a time.

The listener runs inside publishEvent, on the same thread.

So the answer to the question is no: we do not need to extend ApplicationEvent today. Plain records keep our domain code free of Spring types, which also makes the events easy to reuse later, for example as messages on a broker.

4Listeners Run on the Publishing Thread

By default the context hands events to a SimpleApplicationEventMulticaster without a task executor. It calls each matching listener one after another, on the thread that called publishEvent(). That explains the log order above, and it has three consequences worth knowing.

First, a slow listener makes the publisher slow: if the email listener takes two seconds, place() takes two seconds longer. Second, an exception thrown by a listener propagates out of publishEvent() into OrderService. Third, if place() runs in a transaction, the listener runs inside the same transaction, because it is on the same thread. When we want different behaviour, we use @Async for a background listener or @TransactionalEventListener to run after the commit.

5Publishing a Generic Event

A generic event such as EntityCreated<Order> needs one extra step, because Java erases the type argument at runtime: the object only knows it is an EntityCreated. The event can tell Spring its full type by implementing ResolvableTypeProvider.

Java
public record EntityCreated<T>(T entity) implements ResolvableTypeProvider {
@Override
public ResolvableType getResolvableType() {
return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forInstance(entity));
}
}
@EventListener void onOrder(EntityCreated<Order> e) { } // called for EntityCreated(order)
@EventListener void onCustomer(EntityCreated<Customer> e) { } // not called for it

Without getResolvableType(), Spring cannot tell EntityCreated<Order> from EntityCreated<Customer>, and the listeners would not match as we intend. With it, Spring compares the full generic type with each listener's parameter type.

6Follow-up questions

What does publishEvent do if no listener matches?

Nothing visible. The multicaster finds no listeners for the type and returns, without an error or a log message. That is by design, since the publisher should not care who listens, but it means a typo in a listener's parameter type fails silently, so a test that asserts the listener ran is worth having.

How would you publish a generic event such as EntityCreated<Order> so listeners can match on the type argument?

Make the event implement ResolvableTypeProvider and return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forInstance(entity)). Spring then knows the full type at runtime, and a listener declared for EntityCreated<Order> receives only events carrying an Order.

Should a listener be allowed to change the event?

It is better not to. Several listeners receive the same object, and if one changes it, the others see different data depending on the order they run in. Records make events immutable, which avoids the problem.

7Conclusion

In this answer, we saw that we publish an event by injecting ApplicationEventPublisher and calling publishEvent(). Since Spring 4.2 the event can be any object, usually a record; Spring wraps it in a PayloadApplicationEvent and unwraps the payload for listeners that declare its type. Listeners run synchronously on the publishing thread by default, so they add to the caller's time, can throw into it and share its transaction, and generic events need ResolvableTypeProvider to keep their type argument.

What to say in the interviewAbout 70 seconds aloud

Open with

Today I inject ApplicationEventPublisher into the class that publishes and call publishEvent with the event object. Since Spring 4.2 I do not need to extend ApplicationEvent, so my events are plain records like OrderPlaced with an order id and a total, which keeps Spring types out of the domain.

Then, if they want more

Internally Spring wraps a non-ApplicationEvent object in a PayloadApplicationEvent, but the listener does not see that: I write a method with @EventListener and an OrderPlaced parameter, and Spring unwraps the payload and passes the record. If I need the wrapper, for the timestamp for example, I can declare PayloadApplicationEvent of OrderPlaced instead.

One thing I always point out is that by default listeners run synchronously on the publishing thread. So publishEvent only returns after every listener has finished, a slow listener slows the publisher, an exception in a listener propagates back to the caller, and the listener runs inside the caller's transaction. If no listener matches, nothing happens and nothing is logged.

For generic events like EntityCreated of Order, the event should implement ResolvableTypeProvider so Spring knows the type argument at runtime and only matching listeners are called.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do you publish an application event in Spring today, and do you still need to extend...": https://scrivio-interview-library.netlify.app/spring-boot/spring-events/33d7a70957/