-
Notifications
You must be signed in to change notification settings - Fork 38.5k
ApplicationContext.refresh() causes stale listeners to be added to ApplicationEventMulticaster #22325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@wilkinsona since this one addresses a side effect of #21858 which you requested, could you please double-check that the revision (in master and 5.1.x at this point) still works for your purposes, exposing all application listeners that you intend to see from |
This looks good to me with 5.1.5 snapshots. The sample provided with spring-projects/spring-boot#14490 stops as expected. |
Alright, good to hear. Thanks for the immediate feedback! |
@eknizat since this is available in |
The 5.1.5 snapshot works as expected. Thanks. |
This issue is closed, but @jhoeller can you check this line, please condition I have the following problem: on the application startup there is a call of
There are this.applicationListeners is an empty collection.
but it's an empty LinkedHashSet, not null, so then it comes to All in all, is there a reason to add isEmpty checking too? Or what will be the better decision? |
The problem is caused by the change in Spring AbstractApplicationContext, in particular how it handles the addition of ApplicationListener objects.
In short, all of the registered ApplicationListener objects are retained even after application context is refreshed,
at which point the stale listeners become unusable as AbstractLifecycleBean(s) would have had their destroy methods invoked.
The change that caused this problem:
#21858
Commit:
c8c0737
With this commit all listeners are added to AbstractApplicationContext's list applicationListeners instead of just adding them to an instance of ApplicationEventMulticaster.
This causes stale listeners to be added to a new instance of ApplicationEventMulticaster when the AbstractApplicationContext is refreshed via its refresh() method.
The refresh() method creates a new ApplicationEventMulticaster but then it adds all of the stale listeners (by registerListeners()) from its list of listeners.
Subsequently, when the refresh finishes (AbstractApplicationContext.finishRefresh()), a new instance of ApplicationEventMulticaster publishes the refresh event too all registered listeners, including the stale listeners whose destroy methods were called as part of the refresh.
The text was updated successfully, but these errors were encountered: