Skip to content

Commit 3c9bd31

Browse files
committed
Move getListenerId method to Smart/GenericApplicationListener
See gh-26638
1 parent 4b80ef2 commit 3c9bd31

File tree

8 files changed

+53
-66
lines changed

8 files changed

+53
-66
lines changed

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

+2-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.EventListener;
2020
import java.util.function.Consumer;
21-
import java.util.function.Predicate;
2221

2322
/**
2423
* Interface to be implemented by application event listeners.
@@ -36,6 +35,8 @@
3635
* @param <E> the specific {@code ApplicationEvent} subclass to listen to
3736
* @see org.springframework.context.ApplicationEvent
3837
* @see org.springframework.context.event.ApplicationEventMulticaster
38+
* @see org.springframework.context.event.SmartApplicationListener
39+
* @see org.springframework.context.event.GenericApplicationListener
3940
* @see org.springframework.context.event.EventListener
4041
*/
4142
@FunctionalInterface
@@ -47,17 +48,6 @@ public interface ApplicationListener<E extends ApplicationEvent> extends EventLi
4748
*/
4849
void onApplicationEvent(E event);
4950

50-
/**
51-
* Return an optional identifier for the listener.
52-
* <p>The default value is an empty String.
53-
* @since 5.3.5
54-
* @see org.springframework.context.event.EventListener#id
55-
* @see org.springframework.context.event.ApplicationEventMulticaster#removeApplicationListeners(Predicate)
56-
*/
57-
default String getListenerId() {
58-
return "";
59-
}
60-
6151

6252
/**
6353
* Create a new {@code ApplicationListener} for the given payload consumer.

spring-context/src/main/java/org/springframework/context/event/ApplicationEventMulticaster.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public interface ApplicationEventMulticaster {
7878
* <p>Note: This just applies to instance registrations, not to listeners
7979
* registered by bean name.
8080
* @param predicate the predicate to identify listener instances to remove,
81-
* e.g. checking {@link ApplicationListener#getListenerId()}
81+
* e.g. checking {@link SmartApplicationListener#getListenerId()}
8282
* @since 5.3.5
8383
* @see #addApplicationListener(ApplicationListener)
8484
* @see #removeApplicationListener(ApplicationListener)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -18,46 +18,39 @@
1818

1919
import org.springframework.context.ApplicationEvent;
2020
import org.springframework.context.ApplicationListener;
21-
import org.springframework.core.Ordered;
2221
import org.springframework.core.ResolvableType;
23-
import org.springframework.lang.Nullable;
2422

2523
/**
2624
* Extended variant of the standard {@link ApplicationListener} interface,
2725
* exposing further metadata such as the supported event and source type.
2826
*
2927
* <p>As of Spring Framework 4.2, this interface supersedes the Class-based
3028
* {@link SmartApplicationListener} with full handling of generic event types.
29+
* As of 5.3.5, it formally extends {@link SmartApplicationListener}, adapting
30+
* {@link #supportsEventType(Class)} to {@link #supportsEventType(ResolvableType)}
31+
* with a default method.
3132
*
3233
* @author Stephane Nicoll
34+
* @author Juergen Hoeller
3335
* @since 4.2
3436
* @see SmartApplicationListener
3537
* @see GenericApplicationListenerAdapter
3638
*/
37-
public interface GenericApplicationListener extends ApplicationListener<ApplicationEvent>, Ordered {
39+
public interface GenericApplicationListener extends SmartApplicationListener {
3840

3941
/**
40-
* Determine whether this listener actually supports the given event type.
41-
* @param eventType the event type (never {@code null})
42-
*/
43-
boolean supportsEventType(ResolvableType eventType);
44-
45-
/**
46-
* Determine whether this listener actually supports the given source type.
47-
* <p>The default implementation always returns {@code true}.
48-
* @param sourceType the source type, or {@code null} if no source
42+
* Overrides {@link SmartApplicationListener#supportsEventType(Class)} with
43+
* delegation to {@link #supportsEventType(ResolvableType)}.
4944
*/
50-
default boolean supportsSourceType(@Nullable Class<?> sourceType) {
51-
return true;
45+
@Override
46+
default boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
47+
return supportsEventType(ResolvableType.forClass(eventType));
5248
}
5349

5450
/**
55-
* Determine this listener's order in a set of listeners for the same event.
56-
* <p>The default implementation returns {@link #LOWEST_PRECEDENCE}.
51+
* Determine whether this listener actually supports the given event type.
52+
* @param eventType the event type (never {@code null})
5753
*/
58-
@Override
59-
default int getOrder() {
60-
return LOWEST_PRECEDENCE;
61-
}
54+
boolean supportsEventType(ResolvableType eventType);
6255

6356
}

spring-context/src/main/java/org/springframework/context/event/GenericApplicationListenerAdapter.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @since 3.0
3737
* @see org.springframework.context.ApplicationListener#onApplicationEvent
3838
*/
39-
public class GenericApplicationListenerAdapter implements GenericApplicationListener, SmartApplicationListener {
39+
public class GenericApplicationListenerAdapter implements GenericApplicationListener {
4040

4141
private static final Map<Class<?>, ResolvableType> eventTypeCache = new ConcurrentReferenceHashMap<>();
4242

@@ -64,15 +64,13 @@ public void onApplicationEvent(ApplicationEvent event) {
6464
this.delegate.onApplicationEvent(event);
6565
}
6666

67-
@Override
68-
public String getListenerId() {
69-
return this.delegate.getListenerId();
70-
}
71-
7267
@Override
7368
@SuppressWarnings("unchecked")
7469
public boolean supportsEventType(ResolvableType eventType) {
75-
if (this.delegate instanceof SmartApplicationListener) {
70+
if (this.delegate instanceof GenericApplicationListener) {
71+
return ((GenericApplicationListener) this.delegate).supportsEventType(eventType);
72+
}
73+
else if (this.delegate instanceof SmartApplicationListener) {
7674
Class<? extends ApplicationEvent> eventClass = (Class<? extends ApplicationEvent>) eventType.resolve();
7775
return (eventClass != null && ((SmartApplicationListener) this.delegate).supportsEventType(eventClass));
7876
}
@@ -81,11 +79,6 @@ public boolean supportsEventType(ResolvableType eventType) {
8179
}
8280
}
8381

84-
@Override
85-
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
86-
return supportsEventType(ResolvableType.forClass(eventType));
87-
}
88-
8982
@Override
9083
public boolean supportsSourceType(@Nullable Class<?> sourceType) {
9184
return !(this.delegate instanceof SmartApplicationListener) ||
@@ -97,6 +90,12 @@ public int getOrder() {
9790
return (this.delegate instanceof Ordered ? ((Ordered) this.delegate).getOrder() : Ordered.LOWEST_PRECEDENCE);
9891
}
9992

93+
@Override
94+
public String getListenerId() {
95+
return (this.delegate instanceof SmartApplicationListener ?
96+
((SmartApplicationListener) this.delegate).getListenerId() : "");
97+
}
98+
10099

101100
@Nullable
102101
private static ResolvableType resolveDeclaredEventType(ApplicationListener<ApplicationEvent> listener) {

spring-context/src/main/java/org/springframework/context/event/SmartApplicationListener.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -59,4 +59,15 @@ default int getOrder() {
5959
return LOWEST_PRECEDENCE;
6060
}
6161

62+
/**
63+
* Return an optional identifier for the listener.
64+
* <p>The default value is an empty String.
65+
* @since 5.3.5
66+
* @see EventListener#id
67+
* @see ApplicationEventMulticaster#removeApplicationListeners
68+
*/
69+
default String getListenerId() {
70+
return "";
71+
}
72+
6273
}

spring-context/src/main/java/org/springframework/context/event/SourceFilteringListener.java

+6-11
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* @author Stephane Nicoll
3535
* @since 2.0.5
3636
*/
37-
public class SourceFilteringListener implements GenericApplicationListener, SmartApplicationListener {
37+
public class SourceFilteringListener implements GenericApplicationListener {
3838

3939
private final Object source;
4040

@@ -74,21 +74,11 @@ public void onApplicationEvent(ApplicationEvent event) {
7474
}
7575
}
7676

77-
@Override
78-
public String getListenerId() {
79-
return (this.delegate != null ? this.delegate.getListenerId() : "");
80-
}
81-
8277
@Override
8378
public boolean supportsEventType(ResolvableType eventType) {
8479
return (this.delegate == null || this.delegate.supportsEventType(eventType));
8580
}
8681

87-
@Override
88-
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
89-
return supportsEventType(ResolvableType.forType(eventType));
90-
}
91-
9282
@Override
9383
public boolean supportsSourceType(@Nullable Class<?> sourceType) {
9484
return (sourceType != null && sourceType.isInstance(this.source));
@@ -99,6 +89,11 @@ public int getOrder() {
9989
return (this.delegate != null ? this.delegate.getOrder() : Ordered.LOWEST_PRECEDENCE);
10090
}
10191

92+
@Override
93+
public String getListenerId() {
94+
return (this.delegate != null ? this.delegate.getListenerId() : "");
95+
}
96+
10297

10398
/**
10499
* Actually process the event, after having filtered according to the

spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ public void simpleEventJavaConfig() {
112112
this.eventCollector.assertEvent(listener, event);
113113
this.eventCollector.assertTotalEventsCount(1);
114114

115-
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(
116-
l -> l.getListenerId().contains("TestEvent"));
115+
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l ->
116+
l instanceof SmartApplicationListener && ((SmartApplicationListener) l).getListenerId().contains("TestEvent"));
117117
this.eventCollector.clear();
118118
this.context.publishEvent(event);
119119
this.eventCollector.assertNoEventReceived(listener);
@@ -133,8 +133,8 @@ public void simpleEventXmlConfig() {
133133
this.eventCollector.assertEvent(listener, event);
134134
this.eventCollector.assertTotalEventsCount(1);
135135

136-
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(
137-
l -> l.getListenerId().contains("TestEvent"));
136+
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l ->
137+
l instanceof SmartApplicationListener && ((SmartApplicationListener) l).getListenerId().contains("TestEvent"));
138138
this.eventCollector.clear();
139139
this.context.publishEvent(event);
140140
this.eventCollector.assertNoEventReceived(listener);
@@ -151,8 +151,8 @@ public void metaAnnotationIsDiscovered() {
151151
this.eventCollector.assertEvent(bean, event);
152152
this.eventCollector.assertTotalEventsCount(1);
153153

154-
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(
155-
l -> l.getListenerId().equals("foo"));
154+
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l ->
155+
l instanceof SmartApplicationListener && ((SmartApplicationListener) l).getListenerId().equals("foo"));
156156
this.eventCollector.clear();
157157
this.context.publishEvent(event);
158158
this.eventCollector.assertNoEventReceived(bean);

spring-tx/src/main/java/org/springframework/transaction/event/TransactionalApplicationListener.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@ default int getOrder() {
6464
* <p>It might be necessary for specific completion callback implementations
6565
* to provide a specific id, whereas for other scenarios an empty String
6666
* (as the common default value) is acceptable as well.
67-
* @see ApplicationListener#getListenerId()
67+
* @see org.springframework.context.event.SmartApplicationListener#getListenerId()
6868
* @see TransactionalEventListener#id
6969
* @see #addCallback
7070
*/
71-
@Override
7271
default String getListenerId() {
7372
return "";
7473
}

0 commit comments

Comments
 (0)