Clarify registration of ApplicationListener

Spring Boot fires event very early in the application lifecycle and we
should make crystal clear that a regular `@Bean` registration cannot be
used to register a listener on them.

Closes gh-5061
pull/5146/head
Stephane Nicoll 9 years ago
parent 74bff1f275
commit 6ea74501e7

@ -162,11 +162,26 @@ Javadoc] for full details.
=== Application events and listeners === Application events and listeners
In addition to the usual Spring Framework events, such as In addition to the usual Spring Framework events, such as
{spring-javadoc}/context/event/ContextRefreshedEvent.{dc-ext}[`ContextRefreshedEvent`], {spring-javadoc}/context/event/ContextRefreshedEvent.{dc-ext}[`ContextRefreshedEvent`],
a `SpringApplication` sends some additional application events. Some events are actually a `SpringApplication` sends some additional application events.
triggered before the `ApplicationContext` is created.
You can register event listeners in a number of ways, the most common being [NOTE]
`SpringApplication.addListeners(...)` method. ====
Some events are actually triggered before the `ApplicationContext` is created so you
cannot register a listener on those as a `@Bean`. You can register them via the
`SpringApplication.addListeners(...)` or `SpringApplicationBuilder.listeners(...)`
methods.
If you want those listeners to be registered automatically regardless of the way the
application is created you can add a `META-INF/spring.factories` file to your project
and reference your listener(s) using the `org.springframework.context.ApplicationListener`
key.
[indent=0]
----
org.springframework.context.ApplicationListener=com.example.project.MyListener
----
====
Application events are sent in the following order, as your application runs: Application events are sent in the following order, as your application runs:

Loading…
Cancel
Save