The name of the method org.springframework.boot.context.event.EventPublishingRunListener # contextLoaded and the name of the broadcast event type ApplicationPreparedEvent are confusing.Because there is already a org.springframework.boot.context.event.EventPublishingRunListener # contextPrepared method, but it broadcasts theApplicationContextInitializedEvent event...

Comment From: wilkinsona

Thanks for the report but it's not clear to me why you find the methods confusing. The methods on EventPublishingRunListener are defined by SpringApplicationRunListener. There are two different pieces in play – the SpringApplication and the ApplicationContext associated with it. In the events and method names, they are referred to as application and context respectively.

The javadoc of contextPrepared describes the method as the following:

Called once the ApplicationContext has been created and prepared, but before sources have been loaded.

The javadoc of contextLoaded describes the method as the following:

Called once the application context has been loaded but before it has been refreshed.

During application startup, contextPrepared is broadcast before contextLoaded.

contextPrepared indicates that the application context has been prepared. This means that it has been created, its environment configured, and any ApplicationContextInitializers have been called. Once this initialization is complete, the ApplicationContextInitializedEvent is published.

contextLoaded indicates that the sources configured on SpringApplication have been loaded and their bean definitions are now known to the application context. At this point the application is fully prepared and its context is ready to be refreshed. The ApplicationPreparedEvent is published at this point.

Hopefully the above helps to explain what is happening. Perhaps you could expand a little bit on what you found confusing? We may be able to make some improvements to the javadoc.

Comment From: CodingSinger

Thank you for your reply, I mean why not publish ApplicationPreparedEvent in methodcontextPrepared, and your reply means that ApplicationPreparedEvent means thatApplicationContext is ready to refresh? And the context in the contextPrepared method does not meanApplicationContext?

Comment From: wilkinsona

why not publish ApplicationPreparedEvent in method contextPrepared

Preparing the context is only part of preparing the application. The application is not prepared until the sources have been loaded into the context. This happens after the context has been prepared and is ready to have sources loaded.

ApplicationPreparedEvent means that ApplicationContext is ready to refresh

Yes.

the context in the contextPrepared method does not mean ApplicationContext

No, it means ApplicationContext.

Comment From: CodingSinger

This is a good answer for me, I already understand what you mean. Thanks.