I would like to register and unregister Spring ApplicationListeners dynamically at run-time and not in a Spring config file.
If I can't remove them dynamically, I'll have a memory leak.
Here's my best guess:
I could call AbstractApplicationContext.getApplicationEventMulticaster().add/removeApplicationListener().
Is that the recommended method?
Does anyone remove listeners dynamically?
The following works. This is especially useful for prototype beans that implement ApplicationListener and are created/destroyed frequently. If you don't unregister them, you'll end up with a memory leak.
ApplicationEventMulticaster aem = context.getBean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
aem.removeApplicationListener(appListener);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With