Skip to content

ApplicationContext.refresh() causes stale listeners to be added to ApplicationEventMulticaster  #22325

Closed
@eknidev

Description

@eknidev

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.

Metadata

Metadata

Assignees

Labels

status: backportedAn issue that has been backported to maintenance branchestype: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions