Order ApplicationListener using @Order
AnnotationListener implementations can now be ordered either using the `@Order` annotation or by implementing the Ordered interface. Issue: SPR-12410
This commit is contained in:
parent
67934a22e2
commit
8eb7beebc8
|
|
@ -30,8 +30,8 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||||
import org.springframework.context.ApplicationEvent;
|
import org.springframework.context.ApplicationEvent;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.core.OrderComparator;
|
|
||||||
import org.springframework.core.ResolvableType;
|
import org.springframework.core.ResolvableType;
|
||||||
|
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
|
|
@ -234,7 +234,7 @@ public abstract class AbstractApplicationEventMulticaster
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
OrderComparator.sort(allListeners);
|
AnnotationAwareOrderComparator.sort(allListeners);
|
||||||
return allListeners;
|
return allListeners;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -349,7 +349,7 @@ public abstract class AbstractApplicationEventMulticaster
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
OrderComparator.sort(allListeners);
|
AnnotationAwareOrderComparator.sort(allListeners);
|
||||||
return allListeners;
|
return allListeners;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ import org.springframework.context.support.AbstractApplicationContext;
|
||||||
import org.springframework.context.support.StaticApplicationContext;
|
import org.springframework.context.support.StaticApplicationContext;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.ResolvableType;
|
import org.springframework.core.ResolvableType;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.scheduling.support.TaskUtils;
|
import org.springframework.scheduling.support.TaskUtils;
|
||||||
import org.springframework.tests.sample.beans.TestBean;
|
import org.springframework.tests.sample.beans.TestBean;
|
||||||
|
|
||||||
|
|
@ -168,6 +169,19 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
|
||||||
smc.multicastEvent(new MyOtherEvent(this));
|
smc.multicastEvent(new MyOtherEvent(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void orderedListenersWithAnnotation() {
|
||||||
|
MyOrderedListener3 listener1 = new MyOrderedListener3();
|
||||||
|
MyOrderedListener4 listener2 = new MyOrderedListener4(listener1);
|
||||||
|
|
||||||
|
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
|
||||||
|
smc.addApplicationListener(listener2);
|
||||||
|
smc.addApplicationListener(listener1);
|
||||||
|
|
||||||
|
smc.multicastEvent(new MyEvent(this));
|
||||||
|
smc.multicastEvent(new MyOtherEvent(this));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void proxiedListeners() {
|
public void proxiedListeners() {
|
||||||
|
|
@ -396,4 +410,31 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Order(5)
|
||||||
|
public static class MyOrderedListener3 implements ApplicationListener<ApplicationEvent> {
|
||||||
|
|
||||||
|
public final Set<ApplicationEvent> seenEvents = new HashSet<ApplicationEvent>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ApplicationEvent event) {
|
||||||
|
this.seenEvents.add(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Order(50)
|
||||||
|
public static class MyOrderedListener4 implements ApplicationListener<MyEvent> {
|
||||||
|
|
||||||
|
private final MyOrderedListener3 otherListener;
|
||||||
|
|
||||||
|
public MyOrderedListener4(MyOrderedListener3 otherListener) {
|
||||||
|
this.otherListener = otherListener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(MyEvent event) {
|
||||||
|
assertTrue(otherListener.seenEvents.contains(event));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue