Skip to content

Commit 8eb7bee

Browse files
committed
Order ApplicationListener using @order
AnnotationListener implementations can now be ordered either using the `@Order` annotation or by implementing the Ordered interface. Issue: SPR-12410
1 parent 67934a2 commit 8eb7bee

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
3131
import org.springframework.context.ApplicationEvent;
3232
import org.springframework.context.ApplicationListener;
33-
import org.springframework.core.OrderComparator;
3433
import org.springframework.core.ResolvableType;
34+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
3535
import org.springframework.util.ClassUtils;
3636
import org.springframework.util.ObjectUtils;
3737

@@ -234,7 +234,7 @@ private Collection<ApplicationListener<?>> retrieveApplicationListeners(
234234
}
235235
}
236236
}
237-
OrderComparator.sort(allListeners);
237+
AnnotationAwareOrderComparator.sort(allListeners);
238238
return allListeners;
239239
}
240240

@@ -349,7 +349,7 @@ public Collection<ApplicationListener<?>> getApplicationListeners() {
349349
}
350350
}
351351
}
352-
OrderComparator.sort(allListeners);
352+
AnnotationAwareOrderComparator.sort(allListeners);
353353
return allListeners;
354354
}
355355
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.context.support.StaticApplicationContext;
3636
import org.springframework.core.Ordered;
3737
import org.springframework.core.ResolvableType;
38+
import org.springframework.core.annotation.Order;
3839
import org.springframework.scheduling.support.TaskUtils;
3940
import org.springframework.tests.sample.beans.TestBean;
4041

@@ -168,6 +169,19 @@ public void orderedListeners() {
168169
smc.multicastEvent(new MyOtherEvent(this));
169170
}
170171

172+
@Test
173+
public void orderedListenersWithAnnotation() {
174+
MyOrderedListener3 listener1 = new MyOrderedListener3();
175+
MyOrderedListener4 listener2 = new MyOrderedListener4(listener1);
176+
177+
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
178+
smc.addApplicationListener(listener2);
179+
smc.addApplicationListener(listener1);
180+
181+
smc.multicastEvent(new MyEvent(this));
182+
smc.multicastEvent(new MyOtherEvent(this));
183+
}
184+
171185
@Test
172186
@SuppressWarnings("unchecked")
173187
public void proxiedListeners() {
@@ -396,4 +410,31 @@ public void onApplicationEvent(ApplicationEvent event) {
396410
}
397411
}
398412

413+
@Order(5)
414+
public static class MyOrderedListener3 implements ApplicationListener<ApplicationEvent> {
415+
416+
public final Set<ApplicationEvent> seenEvents = new HashSet<ApplicationEvent>();
417+
418+
@Override
419+
public void onApplicationEvent(ApplicationEvent event) {
420+
this.seenEvents.add(event);
421+
}
422+
423+
}
424+
425+
@Order(50)
426+
public static class MyOrderedListener4 implements ApplicationListener<MyEvent> {
427+
428+
private final MyOrderedListener3 otherListener;
429+
430+
public MyOrderedListener4(MyOrderedListener3 otherListener) {
431+
this.otherListener = otherListener;
432+
}
433+
434+
@Override
435+
public void onApplicationEvent(MyEvent event) {
436+
assertTrue(otherListener.seenEvents.contains(event));
437+
}
438+
}
439+
399440
}

0 commit comments

Comments
 (0)