From 6ea74501e756ae588ab837984e2bc8754aaf8ecb Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 2 Feb 2016 08:50:32 +0100 Subject: [PATCH] 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 --- .../main/asciidoc/spring-boot-features.adoc | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 88ac4b99762..24cabb9a276 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -162,11 +162,26 @@ Javadoc] for full details. === Application events and listeners In addition to the usual Spring Framework events, such as {spring-javadoc}/context/event/ContextRefreshedEvent.{dc-ext}[`ContextRefreshedEvent`], -a `SpringApplication` sends some additional application events. Some events are actually -triggered before the `ApplicationContext` is created. +a `SpringApplication` sends some additional application events. -You can register event listeners in a number of ways, the most common being -`SpringApplication.addListeners(...)` method. +[NOTE] +==== +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: