You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Annotation-based event listener does not receive event published in @PostConstruct [SPR-15122] #19689
in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement
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'}
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.
in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement
Activity
snicoll commentedon Jan 4, 2024
The reason for this is that
EventListenerMethodProcessoris aSmartInitializingSingletonthat loops over beans in the context to determine if they have an annotated method in theafterSingletonsInstantiatedphase. This essentially means that the listener is not registered (yet) in thePostConstructphase 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
MyBeanwas processed beforeAnnotationBasedListener, it would not see the event either.Remove disabled test of declined issue