Skip to content

Commit 6ea7450

Browse files
committed
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 spring-projectsgh-5061
1 parent 74bff1f commit 6ea7450

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

+19-4
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,26 @@ Javadoc] for full details.
162162
=== Application events and listeners
163163
In addition to the usual Spring Framework events, such as
164164
{spring-javadoc}/context/event/ContextRefreshedEvent.{dc-ext}[`ContextRefreshedEvent`],
165-
a `SpringApplication` sends some additional application events. Some events are actually
166-
triggered before the `ApplicationContext` is created.
165+
a `SpringApplication` sends some additional application events.
167166

168-
You can register event listeners in a number of ways, the most common being
169-
`SpringApplication.addListeners(...)` method.
167+
[NOTE]
168+
====
169+
Some events are actually triggered before the `ApplicationContext` is created so you
170+
cannot register a listener on those as a `@Bean`. You can register them via the
171+
`SpringApplication.addListeners(...)` or `SpringApplicationBuilder.listeners(...)`
172+
methods.
173+
174+
If you want those listeners to be registered automatically regardless of the way the
175+
application is created you can add a `META-INF/spring.factories` file to your project
176+
and reference your listener(s) using the `org.springframework.context.ApplicationListener`
177+
key.
178+
179+
[indent=0]
180+
----
181+
org.springframework.context.ApplicationListener=com.example.project.MyListener
182+
----
183+
184+
====
170185

171186
Application events are sent in the following order, as your application runs:
172187

0 commit comments

Comments
 (0)