Skip to content

Commit f8c4071

Browse files
committed
Introduce removeApplicationListener method at ApplicationContext level
Closes gh-14023
1 parent 8627bef commit f8c4071

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

spring-context/src/main/java/org/springframework/context/ConfigurableApplicationContext.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -169,6 +169,14 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life
169169
*/
170170
void addApplicationListener(ApplicationListener<?> listener);
171171

172+
/**
173+
* Remove the given ApplicationListener from this context's set of listeners,
174+
* assuming it got registered via {@link #addApplicationListener} before.
175+
* @param listener the ApplicationListener to deregister
176+
* @since 6.0
177+
*/
178+
void removeApplicationListener(ApplicationListener<?> listener);
179+
172180
/**
173181
* Specify the ClassLoader to load class path resources and bean classes with.
174182
* <p>This context class loader will be passed to the internal bean factory.

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -534,6 +534,15 @@ public void addApplicationListener(ApplicationListener<?> listener) {
534534
this.applicationListeners.add(listener);
535535
}
536536

537+
@Override
538+
public void removeApplicationListener(ApplicationListener<?> listener) {
539+
Assert.notNull(listener, "ApplicationListener must not be null");
540+
if (this.applicationEventMulticaster != null) {
541+
this.applicationEventMulticaster.removeApplicationListener(listener);
542+
}
543+
this.applicationListeners.remove(listener);
544+
}
545+
537546
/**
538547
* Return the list of statically specified ApplicationListeners.
539548
*/

0 commit comments

Comments
 (0)