Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Read this listener:

Java
@EventListener(condition = "#event.amount > 1000")
public AuditEntry onPayment(PaymentReceived event) {
return new AuditEntry(event.id());
}

What does the condition do, and what happens to the returned AuditEntry?

The condition is a SpEL expression evaluated for each PaymentReceived event, and the method runs only when it is true, so only payments above 1000 are audited; #event names the event argument. Because the method returns a non-null value, Spring publishes the returned AuditEntry as a new application event, so any listener for AuditEntry is triggered next. A returned collection or array would be published element by element, and none of this return-value publishing happens for @Async listeners.

Why this question is asked

This short listener packs two features into one line that many developers read past: a filtering condition and a return value that is itself an event. Both are useful, and both surprise people when they meet them in someone else's code, which is why interviewers like to show this snippet and ask what it does. In this answer, we will recap @EventListener, look at how the condition is evaluated and which names it can use, see what Spring does with the returned AuditEntry, and cover the cases where that publishing does not happen.

1A Quick Recap of @EventListener

@EventListener marks a bean method as a listener for application events. Spring looks at the method's parameter type, here PaymentReceived, and calls the method whenever an event of that type is published. Behind the scenes, an adapter object called ApplicationListenerMethodAdapter is created for the method at startup; it is what decides, for each event, whether to call our method and what to do with its result. Both parts of our question are handled by that adapter.

2How the Condition Works

The condition attribute holds a SpEL expression, Spring's expression language. Before calling the method, the adapter evaluates the expression against the event. The result is converted to a Boolean; if it is true the method runs, and otherwise the event is silently skipped for this listener.

Java
public record PaymentReceived(String id, BigDecimal amount) {}
@EventListener(condition = "#event.amount > 1000")
public AuditEntry onPayment(PaymentReceived event) {
return new AuditEntry(event.id());
}
publisher.publishEvent(new PaymentReceived("p-1", new BigDecimal("250"))); // listener skipped
publisher.publishEvent(new PaymentReceived("p-2", new BigDecimal("5000"))); // listener runs, returns AuditEntry("p-2")

Notice that #event.amount reads the record's amount() accessor; SpEL property access works with record accessors as well as getters. The comparison between a BigDecimal and the literal 1000 works because SpEL converts the number for the comparison.

The expression can name things in several ways. #event works here because it is the name of the method parameter: Spring exposes every argument as a variable named after its parameter, when the code is compiled with parameter names, which Spring Boot's build plugins enable by default. Arguments are also available by position, as #a0 or #p0, and as the array #root.args. The root object also has an event property, #root.event, but notice that it is the ApplicationEvent Spring delivered, which for a plain record is the PayloadApplicationEvent wrapper, so the amount would be #root.event.payload.amount there. For a record event, naming the parameter is the clearest choice.

3What Happens to the Returned AuditEntry

After calling the method, the adapter looks at the return value. A void method or a null result is simply ignored. Any other value is published as a new application event, exactly as if the method had called publishEvent(new AuditEntry("p-2")) itself. So a second listener can pick it up.

Java
@Component
class AuditWriter {
@EventListener
void on(AuditEntry entry) {
log.info("audit {}", entry.paymentId());
}
}
publisher.publishEvent(new PaymentReceived("p-2", new BigDecimal("5000")));
// audit p-2 (AuditWriter ran because onPayment returned an AuditEntry)
publisher.publishEvent(new PaymentReceived("p-3", new BigDecimal("10")));
// (nothing: the condition was false, so no AuditEntry was returned)
Step throughA listener that returns an event
PublisherMulticasteronPaymentAuditWriter
publishEvent(PaymentReceived p-2)
condition #event.amount > 1000 is true
returns AuditEntry(p-2)
publish AuditEntry as a new event
on(AuditEntry p-2)
publishEvent returns void

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

The return value becomes a new event; it never reaches the original publisher.

This is a neat way to build a small chain: the payment listener decides that an audit is needed and returns the audit fact, and a separate listener decides how to store it. It also has a catch. A reader who sees a method returning AuditEntry may think the value goes back to the publisher; it does not. publishEvent() returns void, and the returned object is only a new event. So in a shared codebase it is worth a short comment on such methods.

If the method returns an array or a Collection, the adapter publishes each element as a separate event, skipping null elements. Returning List.of(new AuditEntry(id), new MetricEvent(id)) publishes two events, which can be handy and can also be a surprise.

4When the Return Value Is Not Published

If we add @Async to the method, it runs on another thread through a proxy. For a method declared to return AuditEntry, the proxy returns to the adapter immediately with no result, so there is nothing to publish, and Spring's documentation states that return-value publishing is not supported for asynchronous listeners. An @Async listener should therefore publish follow-up events itself by injecting ApplicationEventPublisher. Recent versions do publish the completed value of a returned CompletableFuture, but that is easy to misread, so check your version before relying on it.

A @TransactionalEventListener method goes through the same adapter code, so its return value is published too, but only when the listener finally runs, for example after the commit, and by then the original transaction is over, so listeners of the returned event cannot take part in it. In practice, the cleanest rule is to use return values on plain synchronous @EventListener methods, where the chain is easy to follow.

5Follow-up questions

How would you listen to several unrelated event types with one method?

Use the classes attribute: @EventListener({PaymentReceived.class, RefundIssued.class}). The method can then take no parameter at all, or a parameter of a common supertype such as Object, and Spring calls it for either event. If the types share an interface, declaring that interface as the parameter type also works without the attribute.

What if the condition expression has a typo, such as #event.amout?

Spring evaluates the condition only when an event arrives, so the application starts normally and the first matching event fails with a SpEL evaluation exception thrown from publishEvent(). A unit test that publishes one event through the listener catches this early.

Is a condition better than an if statement inside the method?

They behave the same for a synchronous listener. The condition keeps the filter visible in the annotation and avoids calling the method, which matters for @Async listeners because events that fail the condition never take a thread. An if statement is easier to debug and refactor, so for complex rules I prefer code.

6Conclusion

In this answer, we saw that the condition is a SpEL expression evaluated per event, with #event naming the parameter and #a0, #root.args and #root.event also available, so the method runs only for payments above 1000. The returned AuditEntry is published as a new event and triggers any AuditEntry listener, a collection or array is published element by element, null and void results are ignored, and return-value publishing does not apply to @Async listeners.

What to say in the interviewAbout 90 seconds aloud

Open with

The condition is a SpEL expression that Spring evaluates for every PaymentReceived event before calling the method. Only if it is true does the method run, so here only payments with an amount above 1000 are handled.

Then, if they want more

In the expression, #event refers to the event argument by its parameter name; I can also use #a0 or #p0 by position, or #root.args for the argument array, while #root.event is the delivered ApplicationEvent, which for a record is the payload wrapper. The second part is the return value.

Because the method returns a non-null AuditEntry, Spring publishes that object as a new application event, so any listener for AuditEntry runs next, still on the same thread. It does not go back to the original publisher; publishEvent returns void.

If the method returned an array or a collection, each element would be published as its own event, and a null return publishes nothing.

One important exception: if the listener is annotated with @Async, the return value is not published, because the method runs later on another thread, so an async listener has to inject the publisher and publish follow-up events itself.

I like this pattern for small chains, like deciding an audit is needed and letting a separate listener store it, but I add a comment, because readers often expect the return value to go somewhere else.

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 "Read this listener:": https://scrivio-interview-library.netlify.app/spring-boot/spring-events/e7906c7fc0/