Skip to content

Annotation-based event listener does not receive event published in @PostConstruct [SPR-15122] #19689

Closed as not planned

Description

@spring-projects-issues

František Hartman opened SPR-15122 and commented

Annotation-based event listener does not receive event published in @PostConstruct, while old style interface based listener does.

See example project at https://github.com/frant-hartm/spring-application-events

One event is published in @PostConstruct method of another bean. Another event is published from main method after context is initialised. Interface based listener sees both events, annotation based sees only the second one:

Interface based sees event: MyEvent{value='from postConstruct'}
Annotation based sees event: MyEvent{value='from main'}
Interface based sees event: MyEvent{value='from main'}


Affects: 4.3.5

Reference URL: https://github.com/frant-hartm/spring-application-events

Issue Links:

Activity

  1. added
    in: core
    on Jul 26, 2021
  2. snicoll commented on Jan 4, 2024

    @snicoll
    Member

    The reason for this is that EventListenerMethodProcessor is a SmartInitializingSingleton that loops over beans in the context to determine if they have an annotated method in the afterSingletonsInstantiated phase. This essentially means that the listener is not registered (yet) in the PostConstruct phase of any bean.

    IMO it is better this way as it makes things a bit more clear in terms of contracts. The annotation-based use case purely works by accident. If MyBean was processed before AnnotationBasedListener, it would not see the event either.

  3. added
    status: declined
    on Jan 4, 2024
  4. removed this from the 6.x Backlog milestone on Jan 4, 2024
  5. added a commit that references this issue on Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: corestatus: declinedtype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions