Scrivio Interview library
DebuggingIntermediate5 min read · updated 12 November 2026

A team publishes 'service started' to a registry in a ContextRefreshedEvent listener. In tests and in a parent-child context setup the message is sent twice, and sometimes before the server accepts requests. Why, and what should they use?

ContextRefreshedEvent is published for every context refresh, so in a parent-child setup the parent's listener also receives the child's event, and each new test context sends its own. It also fires during the refresh, before the runners have finished and before the application is ready to serve. The team should announce the service on ApplicationReadyEvent or on the readiness AvailabilityChangeEvent, and compare event.getApplicationContext() with their own context if they must filter.

Why this question is asked

Announcing that a service has started sounds trivial, and that is why it is often wired to the first event that looks right. Then the registry sees duplicate announcements and sometimes routes traffic to a service that still refuses it. Interviewers like this question because the bug comes from a precise misunderstanding of what ContextRefreshedEvent means. In this answer, we will recap what the event really signals, reproduce the duplicate in a parent-child setup, see why the timing is too early, and then fix it with the right event and, where needed, a context filter.

1A Quick Recap of ContextRefreshedEvent

ContextRefreshedEvent belongs to Spring Framework, not to Spring Boot. Every ApplicationContext publishes it when its refresh() completes, meaning all its singletons have been created. It says nothing about the application as a whole: it is about one context, and it says only that the context finished building.

Two facts follow from that. First, an application can have more than one context, and each one publishes its own event. Second, events published in a child context are also propagated to its parent context, so a listener in the parent hears the child's event as well. Spring Boot's own startup events, such as ApplicationReadyEvent, are published by SpringApplication once per run, at defined points in the startup sequence.

2Reproducing the Duplicate Message

Here is the team's listener, in a configuration that belongs to the parent context, and a SpringApplicationBuilder that creates a parent and a child.

Java
@Component
class RegistryAnnouncer {
private final RegistryClient registry;
RegistryAnnouncer(RegistryClient registry) { this.registry = registry; }
@EventListener
void on(ContextRefreshedEvent event) {
registry.publish("service started");
}
}
new SpringApplicationBuilder(SharedConfig.class) // parent: holds RegistryAnnouncer
.child(WebConfig.class) // child: web layer
.run(args);
// registry receives: "service started" (parent refreshed)
// registry receives: "service started" (child refreshed, propagated to parent)
Step throughWhy the parent's listener fires twice
Parent contextChild contextRegistryAnnouncerRegistry
refresh() completes
ContextRefreshedEvent (parent)
service started
refresh() completes
event propagated to parent
ContextRefreshedEvent (child)
service started, again

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

The parent refreshes first and the listener fires. Then the child refreshes, publishes its own ContextRefreshedEvent, and that event is propagated to the parent, where the same listener fires again. In tests the pattern is similar: every distinct test configuration creates and refreshes a new context, and each refresh sends another message, so a test run can announce the service many times to a real registry.

3Why It Is Also Too Early

The second symptom, the message arriving before the server accepts requests, comes from the timing. ContextRefreshedEvent is published at the end of the refresh, but Spring Boot's startup continues after that: ApplicationStartedEvent, then the ApplicationRunner and CommandLineRunner beans, and only then ApplicationReadyEvent and readiness ACCEPTING_TRAFFIC.

If a runner warms a cache for 30 seconds, the registry announces the service 30 seconds before it is ready, and in a parent-child setup the parent's event can fire before the child web context has even started its server. Clients that trust the registry then send requests that fail or wait. The announcement should mean ready to serve, and ContextRefreshedEvent never promised that.

4The Fix: Use ApplicationReadyEvent or Readiness

Spring Boot has a signal that matches the intent. ApplicationReadyEvent is published once, after the runners, when the application is ready. Even better, the AvailabilityChangeEvent for readiness tracks the real state: it says ACCEPTING_TRAFFIC when the application can serve and REFUSING_TRAFFIC when it cannot, so the announcer can also withdraw the service at shutdown.

Java
@Component
class RegistryAnnouncer {
private final RegistryClient registry;
private final ApplicationContext context;
RegistryAnnouncer(RegistryClient registry, ApplicationContext context) {
this.registry = registry; this.context = context;
}
@EventListener
void on(AvailabilityChangeEvent<ReadinessState> event) {
if (event.getSource() instanceof ApplicationContext source && source != context) return; // another context
switch (event.getState()) {
case ACCEPTING_TRAFFIC -> registry.publish("service ready"); // once, after runners
case REFUSING_TRAFFIC -> registry.publish("service unavailable");
}
}
}

Notice the filter. In a context hierarchy, Boot's events can still reach the parent from the child, so a listener that must act once compares the context the event came from with its own. For ApplicationReadyEvent and ContextRefreshedEvent, event.getApplicationContext() gives that context directly, and comparing it with the injected ApplicationContext is the usual check. The same comparison is the minimal fix if a team must keep ContextRefreshedEvent for some reason.

5Keeping Tests Away From the Registry

Using the right event fixes the duplicates, but tests still start contexts and still become ready, so they still announce. The announcer should not run in tests at all. The simplest way is a property switch on the bean, off by default and on in production configuration.

Java
@Component
@ConditionalOnProperty(name = "registry.announce.enabled", havingValue = "true")
class RegistryAnnouncer { /* as above */ }
# application-prod.yml
# registry.announce.enabled: true
# tests: property absent -> bean not created -> no messages

Better still, as discussed in platform design, a Kubernetes Service already tracks ready pods through the readiness probe, which often removes the need for an announcer. When one is still needed, readiness is the right trigger.

6Follow-up questions

Why does an event from the child reach a listener in the parent?

Because AbstractApplicationContext publishes every event to its own listeners and then to its parent context. This lets parent beans observe the whole hierarchy, but it means a parent listener must filter if it only cares about its own context.

Is ContextRefreshedEvent ever the right choice?

Yes, for work that is truly about one context being built, inside framework code, or in plain Spring applications without Boot. For application-level readiness in Boot, ApplicationReadyEvent or the readiness state is the correct signal.

Does ContextRefreshedEvent fire more than once for the same context?

It can, if a context that supports it is refreshed again, though Boot's contexts normally refresh once. The more common duplicates come from multiple contexts in a hierarchy and from test contexts.

7Conclusion

In this answer, we saw that ContextRefreshedEvent is a Spring Framework event published by every context when its refresh completes, and that child events propagate to the parent, so a parent listener fires once per context; tests add more contexts. It also fires before the runners and before readiness. The fix is to announce on ApplicationReadyEvent or the readiness AvailabilityChangeEvent, compare the event's context with our own when there is a hierarchy, and keep the announcer off in tests.

What to say in the interviewAbout 70 seconds aloud

Open with

ContextRefreshedEvent is a Spring Framework event that each ApplicationContext publishes when its own refresh completes. It is about one context being built, not the application being ready. That explains both symptoms.

Then, if they want more

In a parent-child setup the parent refreshes and fires the event, then the child refreshes and its event is propagated up to the parent, so the listener in the parent fires a second time. In tests, every new test context refreshes and sends its own message.

And on timing, the event fires at the end of the refresh, but Boot then still runs ApplicationStartedEvent and the runners before ApplicationReadyEvent and readiness ACCEPTING_TRAFFIC, so the registry hears about the service before it can serve. I would switch the announcer to ApplicationReadyEvent, or better to the readiness AvailabilityChangeEvent, so it also says unavailable at shutdown.

If the app uses a context hierarchy, I would compare event.getApplicationContext or the event source with the injected context so it fires only for its own context. And I would put the announcer behind a property so tests do not talk to the registry, or remove it if Kubernetes discovery already covers it.

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.
Official documentation
Spring Boot reference
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 "A team publishes 'service started' to a registry in a ContextRefreshedEvent listener. In...": https://scrivio-interview-library.netlify.app/spring-boot/boot-startup/36b08da828/