Quick revision
The short answer
ApplicationStartingEvent is fired at the very start of SpringApplication.run(), before the ApplicationContext exists, so there are no beans yet and a @Component listener cannot receive it. ApplicationReadyEvent is fired after the context has been refreshed, which is why that listener works. Early Boot events need listeners registered on SpringApplication itself, with addListeners() or through META-INF/spring.factories under the ApplicationListener key; the same applies to ApplicationEnvironmentPreparedEvent and ApplicationContextInitializedEvent.
What to remember
In this answer, we saw that @EventListener methods are registered only during the context refresh, after their beans are created. ApplicationStartingEvent, ApplicationEnvironmentPreparedEvent, ApplicationContextInitializedEvent and ApplicationPreparedEvent are all published before that, so bean listeners never hear them, while ApplicationReadyEvent comes after the refresh and works. Early events need an ApplicationListener added through SpringApplication.addListeners or listed in META-INF/spring.factories.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
ApplicationStartingEvent is fired at the very start of SpringApplication.run(), before the ApplicationContext exists, so there are no beans yet and a @Component listener cannot receive it. ApplicationReadyEvent is fired after the context has been refreshed, which is why that listener works. Early Boot events need listeners registered on SpringApplication itself, with addListeners() or through META-INF/spring.factories under the ApplicationListener key; the same applies to ApplicationEnvironmentPreparedEvent and ApplicationContextInitializedEvent.
Why this question is asked
This is a classic puzzle because the two listeners look identical and live in the same class, yet one is silent. The explanation is all about timing: when each event is fired compared with when beans are created. Interviewers ask it to see whether we know Spring Boot's startup sequence well enough to reason about it. In this answer, we will recap how @EventListener beans are registered, walk through the order of Boot's startup events, see exactly why the early listener cannot work, and then register it the right way.
1How @EventListener Beans Get Registered
An @EventListener method only becomes a listener after Spring has found it. That happens during the refresh of the application context: Spring creates the singleton beans, and then a component called EventListenerMethodProcessor scans them for @EventListener methods and registers an adapter for each one with the context.
So a bean listener can only hear events that are published after the context has been refreshed far enough for that registration to happen. Anything published earlier goes to whatever listeners exist at that moment, and our @Component is not among them because it has not even been instantiated.
2The Order of Boot's Startup Events
SpringApplication.run() publishes a series of events as it starts. Following them in order shows where the line falls.
| Event | What exists at that moment | @EventListener bean receives it? |
|---|---|---|
ApplicationStartingEvent | only SpringApplication, no environment | no |
ApplicationEnvironmentPreparedEvent | Environment, no context | no |
ApplicationContextInitializedEvent | context created, no bean definitions loaded | no |
ApplicationPreparedEvent | bean definitions loaded, beans not created | no |
ContextRefreshedEvent | all singletons created | yes |
ApplicationStartedEvent | refreshed, runners not yet called | yes |
ApplicationReadyEvent | runners called, ready for traffic | yes |
ContextRefreshedEvent happens before bean listeners are registered.3Why the Starting Listener Is Silent
Here is the class from the question.
When run() publishes ApplicationStartingEvent, there is no ApplicationContext yet, so there is no StartupLogger bean and no adapter for its starting() method. Boot delivers the event only to the listeners SpringApplication already knows about, the ones loaded from spring.factories or added in code. Later, when the context refreshes and StartupLogger is created, its starting() method is registered as a listener, but the event it wants has already happened and will never be published again. No error is reported, because a listener that never matches is not an error.
ApplicationReadyEvent, on the other hand, is published at the end of run(), long after StartupLogger was created and registered. So the same class receives one event and silently misses the other. The same is true for ApplicationEnvironmentPreparedEvent, ApplicationContextInitializedEvent and ApplicationPreparedEvent: all of them happen before bean listeners exist.
4Fix 1: Register the Listener on SpringApplication
For early events, the listener must be an ApplicationListener object handed to SpringApplication before run() starts. It is a plain object, not a bean, so it cannot use dependency injection.
Notice the comment about logging. At ApplicationStartingEvent, Boot has not yet configured the logging system from our properties, so output may not follow our log format. That is another reason this event is meant for very early infrastructure work rather than application logic.
5Fix 2: Register Through spring.factories
When the listener lives in a library and should apply to every application that uses it, we cannot edit each main() method. Instead, the library lists the listener in META-INF/spring.factories, which SpringApplication reads when it is constructed.
This is the one place where spring.factories is still the right file in Spring Boot 3 and 4. Auto-configuration classes moved to the AutoConfiguration.imports file, but early application listeners and environment post-processors are still loaded from spring.factories. Check your version's documentation if you are on a release that changes this. The practical rule is simple: if the event is published before the context refresh, the listener must be registered on SpringApplication, by code or by spring.factories; if it is published after, an @EventListener bean is the easiest choice.
6Follow-up questions
Would implementing ApplicationListener on the @Component help?
No. The problem is not the style of listener but the fact that the bean does not exist yet. An ApplicationListener bean is registered during refresh too, so it also misses the early events. It only works when the object is added to SpringApplication before run().
Which event should I use to run code once the application is ready for traffic?
ApplicationReadyEvent, or an ApplicationRunner or CommandLineRunner bean. Runners are called just before ApplicationReadyEvent and are the usual place for startup tasks. If the work is long, start it asynchronously so readiness is not delayed.
How can I check which events my listener receives?
Add a listener for ApplicationEvent, the common supertype, and log each event's class. With SpringApplication.addListeners it shows the whole startup sequence, and as a bean it shows only the events from the refresh onwards, which makes the boundary visible.
7Conclusion
In this answer, we saw that @EventListener methods are registered only during the context refresh, after their beans are created. ApplicationStartingEvent, ApplicationEnvironmentPreparedEvent, ApplicationContextInitializedEvent and ApplicationPreparedEvent are all published before that, so bean listeners never hear them, while ApplicationReadyEvent comes after the refresh and works. Early events need an ApplicationListener added through SpringApplication.addListeners or listed in META-INF/spring.factories.
Open with
It is a timing problem. An @EventListener method only becomes a listener when Spring creates the bean during the context refresh and the EventListenerMethodProcessor registers it. ApplicationStartingEvent is the very first event SpringApplication.run publishes, before the environment is prepared and before the ApplicationContext even exists, so there is no bean and no listener for it.
Then, if they want more
Boot only delivers it to listeners registered on SpringApplication itself. By the time our component is created, that event is long gone, and Spring does not report anything, because a listener that never receives an event is not an error. ApplicationReadyEvent is published at the end of run, after the refresh, so the same bean receives it.
The same restriction applies to ApplicationEnvironmentPreparedEvent, ApplicationContextInitializedEvent and ApplicationPreparedEvent. To listen to an early event, I would write a class that implements ApplicationListener of ApplicationStartingEvent and add it with SpringApplication.addListeners before calling run, or, for a library, list it in META-INF/spring.factories under the ApplicationListener key, which Boot still reads for early listeners.
Such a listener is a plain object without dependency injection, and logging may not be configured yet, so it should do only small infrastructure work.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3
- 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 Framework reference, Core technologies
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 @Component with an @EventListener method for ApplicationStartingEvent never logs...": https://scrivio-interview-library.netlify.app/spring-boot/spring-events/900c499550/