Skip to content

Conversation

khj68
Copy link
Contributor

@khj68 khj68 commented Aug 17, 2025

Description

This PR improves the Javadoc of ApplicationEvents to better guide developers on its proper usage.

Problem

The current Javadoc mentions field injection with @Autowired before method parameter injection, which creates the impression that ApplicationEvents is a regular Spring bean eligible for dependency injection. This confuses developers who then attempt to use constructor injection and find it doesn't work.

Solution

Reordered and clarified the usage instructions to:

  1. Recommend method parameter injection as the primary approach - since ApplicationEvents has a per-method lifecycle, this is the most appropriate usage pattern
  2. Clarify that ApplicationEvents is not a general Spring bean - explicitly state that it's specifically designed for use within test methods
  3. Position field injection as an alternative - rather than the primary approach

Changes

  • Reordered the bullet points to show method parameter injection first
  • Added clarification about the per-method lifecycle
  • Added a note that ApplicationEvents is not a general Spring bean

This helps prevent the confusion described in the linked German documentation where developers were puzzled by the inability to use constructor injection.

Closes gh-35297

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.

Closes spring-projectsgh-35297

Signed-off-by: khj68 <[email protected]>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 17, 2025
@sbrannen sbrannen self-assigned this Aug 17, 2025
@sbrannen sbrannen added the in: test Issues in the test module label Aug 17, 2025
@sbrannen sbrannen changed the title Improve Javadoc of ApplicationEvents to clarify preferred usage Improve Javadoc of ApplicationEvents to clarify recommended usage Aug 17, 2025
@sbrannen sbrannen added type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 20, 2025
@sbrannen sbrannen added this to the 6.2.11 milestone Aug 20, 2025
@sbrannen sbrannen changed the title Improve Javadoc of ApplicationEvents to clarify recommended usage Improve documentation for ApplicationEvents to clarify recommended usage Aug 20, 2025
@sbrannen sbrannen closed this in c0b71f8 Aug 20, 2025
Copy link
Contributor

Fixed via c0b71f8

@sbrannen
Copy link
Member

Hi @khj68,

Please note that #35297 was assigned to me, and we typically do not accept PRs for issues assigned to someone.

In any case, this PR served as a good starting point.

Thus I have merged this into main in c0b71f8 and completed the work in 19d5ec6.

Cheers,

Sam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: documentation A documentation task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Javadoc of ApplicationEvents
3 participants