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: - #16245 ApplicationListener-like annotation for consuming application events - #17501 Revisit "ApplicationEventMulticaster not initialized" behavior for early event publication scenarios - #20002 Introduce API to select application listeners
Comment From: snicoll
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.