Skip to content

Remove all ApplicationListenerMethodAdapters on context close [SPR-17456] #21988

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 Nov 2, 2018 · 1 comment
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 Nov 2, 2018

Pramy opened SPR-17456 and commented

I know a bean implement ApplicationListener will be remove from Context when it destroy,

but ApplicationListenerMethodAdapter can't be remove 


1.ParentContext invoke close method

2.Listener destroy

  it did not implements ApplicationListener when use @EventListener

3.NamedContext destroy

  it will invoke close method and publish close event, and parentContext will also publish close
  event, ApplicationListenerMethodAdapter working will invoke conetext#getBean


@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        context.close();
    }

    class Listener implements DisposableBean {

        @EventListener(ContextClosedEvent.class)
        public void close(ApplicationEvent event) {
            System.out.println("---------------invoke listener"+event );
        }
        @Override
        public void destroy() throws Exception {
            System.out.println("--------------------listener destroy-----------------------");
        }
    }


    //1.insure NamedContextFactory initialize before listener
    //2.insure NamedContextFactory destroy after listener
    @Bean
    public Listener listener() {
        //3.initialize context values
        namedContextFactory().getInstance("demo",DemoApplication.class);
        return new Listener();
    }

    @Bean
    public NamedContextFactory<NamedContextFactory.Specification> namedContextFactory() {
        return new NamedContextFactory<NamedContextFactory.Specification>(Config.class
        ,"demo","demo") {};
    }

    @Configuration
    class Config{}
}

Affects: 5.0.9, 5.0.10, 5.1.1, 5.1.2

Reference URL: https://github.com/Pramy/spring-listener-bug

Attachments:

Referenced from: pull request #2006

@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) and removed type: enhancement A general enhancement labels Jan 11, 2019
@jhoeller jhoeller added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 21, 2023
@jhoeller jhoeller self-assigned this Sep 21, 2023
@jhoeller jhoeller added this to the 6.1.0-RC1 milestone Sep 21, 2023
@jhoeller jhoeller changed the title ApplicationListenerMethodAdapter can't be removed after targetBean destroy [SPR-17456] ApplicationListenerMethodAdapter still registered after context close [SPR-17456] Oct 10, 2023
@jhoeller jhoeller changed the title ApplicationListenerMethodAdapter still registered after context close [SPR-17456] Remove all ApplicationListenerMethodAdapters on context close [SPR-17456] Oct 10, 2023
@jhoeller
Copy link
Contributor

There is indeed an issue with not clearing the internal ApplicationEventMulticaster (which may hold on to remaining ApplicationListenerMethodAdapters) on context close. As of 6.1, we reset those internal delegate references to null on close.

That said, the scenario above closes the contexts in the wrong order: You need to close the child context first, then the parent context, since the child may rely on the functionality of the parent still. If you close the parent first as shown above, the child context may still rely on it for all sorts of purposes and therefore fail during its own shutdown procedure.

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

2 participants