Skip to content

Commit c0b71f8

Browse files
khj68sbrannen
authored andcommitted
Improve Javadoc of ApplicationEvents to clarify preferred usage
This commit reorders and clarifies the usage instructions for ApplicationEvents to: 1. Recommend method parameter injection as the primary approach, since ApplicationEvents has a per-method lifecycle 2. Clarify that ApplicationEvents is not a general Spring bean and cannot be constructor-injected 3. Explicitly state that field injection is an alternative approach This addresses confusion where developers expect ApplicationEvents to behave like a regular Spring bean eligible for constructor injection. See gh-35297 Closes gh-35335 Signed-off-by: khj68 <[email protected]>
1 parent 5d214c2 commit c0b71f8

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

spring-test/src/main/java/org/springframework/test/context/event/ApplicationEvents.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
* to be manually registered if you have custom configuration via
3434
* {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners}
3535
* that does not include the default listeners.</li>
36-
* <li>Annotate a field of type {@code ApplicationEvents} with
36+
* <li>With JUnit Jupiter, declare a parameter of type {@code ApplicationEvents}
37+
* in a test or lifecycle method. Since {@code ApplicationEvents} is scoped to the
38+
* lifecycle of the current test method, this is the recommended approach.</li>
39+
* <li>Alternatively, you can annotate a field of type {@code ApplicationEvents} with
3740
* {@link org.springframework.beans.factory.annotation.Autowired @Autowired} and
38-
* use that instance of {@code ApplicationEvents} in your test and lifecycle methods.</li>
39-
* <li>With JUnit Jupiter, you may optionally declare a parameter of type
40-
* {@code ApplicationEvents} in a test or lifecycle method as an alternative to
41-
* an {@code @Autowired} field in the test class.</li>
41+
* use that instance of {@code ApplicationEvents} in your test and lifecycle methods.
42+
* Note that {@code ApplicationEvents} is not a general Spring bean and is specifically
43+
* designed for use within test methods.</li>
4244
* </ul>
4345
*
4446
* @author Sam Brannen

0 commit comments

Comments
 (0)