Skip to content

Introduce removeApplicationListener in ConfigurableApplicationContext #14023

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

Closed
spring-projects-issues opened this issue May 8, 2012 · 7 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented May 8, 2012

Archie Cobbs opened SPR-9387 and commented

I have some object which need to receive some ApplicationEvents that are sent around in my application.

However, these objects have a lifecycle that does not match with the lifecycle of normal beans in the application context (their lifecycle is shorter).

Therefore, I have these beans marked @Configurable, and they are ApplicationContextAware so they can get a reference to the application context (which is assumed to be a ConfigurableApplicationContext.

Then, when they "start" they register as listeners for application events via ConfigurableApplicationContext.addApplicationListener(). However, when they "stop" there is no way for them to unregister themselves as listeners, because there is no corresponding method ConfigurableApplicationContext.removeApplicationListener().

So this request is simply to add ConfigurableApplicationContext.removeApplicationListener().

If there is some more elegant way to do what I'm trying to do please let me know. But just from the face of it, it seems weirdly asymmetrical to have a public addFooListener() method without a corresponding removeFooListener() method.

Thanks.


Affects: 3.1.1

Issue Links:

1 votes, 4 watchers

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented May 9, 2012

Archie Cobbs commented

Additional info: even though these @Configurable beans implement ApplicationListener, they are not automatically registered with the event multicaster, instead triggering this warning in AbstractApplicationContext.java:

Boolean flag = this.singletonNames.get(beanName);
if (Boolean.TRUE.equals(flag)) {
    // singleton bean (top-level or inner): register on the fly
    addApplicationListener((ApplicationListener<?>) bean);
}
else if (flag == null) {
    if (logger.isWarnEnabled() && !containsBean(beanName)) {
        // inner bean with other scope - can't reliably process events
        logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " +
                "but is not reachable for event multicasting by its containing ApplicationContext " +
                "because it does not have singleton scope. Only top-level listener beans are allowed " +
                "to be of non-singleton scope.");
    }
    this.singletonNames.put(beanName, Boolean.FALSE);
}

That is why they have to be manually registered as listeners.

So this is kind-of a Catch-22 situation.

By the way, this issue relates to #9922, which is marked Resolved, but is it really? It seems like the code above means that it's not really resolved.

Also: I accidentally set the Component to SpringAOP instead of SpringCore, but don't have permission to edit it... sorry.

@spring-projects-issues
Copy link
Collaborator Author

Archie Cobbs commented

Related to this. There is an opportunity for a little API cleanup:

  • Why not let ConfigurableApplicationContext extend ApplicationEventMulticaster? Right now there are two separate interfaces declaring an addApplicationListener() method.
  • ApplicationEventMulticaster still needs some genericization, i.e., methods should take parameters of type ApplicationListener<?> instead of ApplicationListener
  • We have both ApplicationEventPublisher.publishEvent() and ApplicationEventMulticaster.multicastEvent(), both doing the same thing. Seems like these methods should have the same name, and ApplicationEventMulticaster should just extend ApplicationEventPublisher.

@spring-projects-issues spring-projects-issues added status: waiting-for-triage An issue we've not yet triaged or decided on type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: enhancement A general enhancement label Jan 11, 2019
@rstoyanchev rstoyanchev added status: bulk-closed An outdated, unresolved issue that's closed in bulk as part of a cleaning process and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 11, 2019
@spring-projects-issues
Copy link
Collaborator Author

Bulk closing outdated, unresolved issues. Please, reopen if still relevant.

@archiecobbs
Copy link

Bulk closing outdated, unresolved issues. Please, reopen if still relevant.

... except Github won't let me reopen it...

@rstoyanchev rstoyanchev reopened this Jan 21, 2019
@rstoyanchev rstoyanchev added status: waiting-for-triage An issue we've not yet triaged or decided on for: team-attention and removed status: bulk-closed An outdated, unresolved issue that's closed in bulk as part of a cleaning process labels Jan 21, 2019
@rstoyanchev rstoyanchev added status: bulk-closed An outdated, unresolved issue that's closed in bulk as part of a cleaning process and removed for: team-attention labels Jun 28, 2019
@sbrannen
Copy link
Member

This feature was also considered in conjunction with the initial implementation for #25616.

@rstoyanchev
Copy link
Contributor

Scheduling for 6.0 with a chance to explore a backport to 5.3.x.

@rstoyanchev rstoyanchev added this to the 6.0.0-M4 milestone Mar 29, 2022
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 29, 2022
@rstoyanchev rstoyanchev changed the title Add new method ConfigurableApplicationContext.removeApplicationListener() [SPR-9387] Introduce removeApplicationListener in ConfigurableApplicationContext Mar 29, 2022
@sbrannen sbrannen removed the status: bulk-closed An outdated, unresolved issue that's closed in bulk as part of a cleaning process label Mar 29, 2022
@archiecobbs
Copy link

Woo-hoo! Just in time for it's 10th birthday on Sunday :)

Seriously, this is why I love Spring - most projects would never bother to fix a 10 year old minor feature request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

6 participants