diff --git a/spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java b/spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java index 29e0daf523..662fdb2bfc 100644 --- a/spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -169,6 +169,14 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life */ void addApplicationListener(ApplicationListener> listener); + /** + * Remove the given ApplicationListener from this context's set of listeners, + * assuming it got registered via {@link #addApplicationListener} before. + * @param listener the ApplicationListener to deregister + * @since 6.0 + */ + void removeApplicationListener(ApplicationListener> listener); + /** * Specify the ClassLoader to load class path resources and bean classes with. *
This context class loader will be passed to the internal bean factory. diff --git a/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java b/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java index 17bc9a7330..3c8b5c8aab 100644 --- a/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -534,6 +534,15 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader this.applicationListeners.add(listener); } + @Override + public void removeApplicationListener(ApplicationListener> listener) { + Assert.notNull(listener, "ApplicationListener must not be null"); + if (this.applicationEventMulticaster != null) { + this.applicationEventMulticaster.removeApplicationListener(listener); + } + this.applicationListeners.remove(listener); + } + /** * Return the list of statically specified ApplicationListeners. */