DATAREST-970 - AnnotationEventHandlerInvoker now considers order of event handler methods.

We now make sure that an @Order annotation on annotated event handler methods are considered and the methods are invoked in the defined order.

Non-annotation-based event handlers don't suffer from the same problem as they're ApplicationListener instances directly so that the container will enforce the correct ordering in case @Order is used or Ordered is implemented.

Some cleanup in EventHandlerMethod.

Original pull request: #248.
This commit is contained in:
Oliver Gierke
2017-01-19 15:05:43 +01:00
parent 2ad00c0dd7
commit 2b9c7847b4
2 changed files with 81 additions and 14 deletions

View File

@@ -20,6 +20,7 @@ import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.core.annotation.Order;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.data.rest.core.domain.Person;
@@ -32,6 +33,7 @@ import org.springframework.util.MultiValueMap;
*
* @author Oliver Gierke
* @author Fabian Trampusch
* @author Joseph Valerio
*/
public class AnnotatedEventHandlerInvokerUnitTests {
@@ -65,6 +67,24 @@ public class AnnotatedEventHandlerInvokerUnitTests {
assertThat(sampleHandler.wasCalled, is(true));
}
@Test // DATAREST-970
public void invokesEventHandlerInOrderMethods() {
SampleOrderEventHandler1 orderHandler1 = new SampleOrderEventHandler1();
SampleOrderEventHandler2 orderHandler2 = new SampleOrderEventHandler2();
AnnotatedEventHandlerInvoker invoker = new AnnotatedEventHandlerInvoker();
invoker.postProcessAfterInitialization(orderHandler1, "orderHandler1");
invoker.postProcessAfterInitialization(orderHandler2, "orderHandler2");
invoker.onApplicationEvent(new BeforeCreateEvent(new Person("Dave", "Matthews")));
assertThat(orderHandler1.wasCalled, is(true));
assertThat(orderHandler2.wasCalled, is(true));
assertThat(orderHandler1.timestamp, is(greaterThan(orderHandler2.timestamp)));
}
@RepositoryEventHandler
static class Sample {
@@ -82,4 +102,32 @@ public class AnnotatedEventHandlerInvokerUnitTests {
wasCalled = true;
}
}
@RepositoryEventHandler
static class SampleOrderEventHandler1 {
boolean wasCalled = false;
long timestamp;
@Order(2)
@HandleBeforeCreate
private void method(Person sample) {
wasCalled = true;
timestamp = System.nanoTime();
}
}
@RepositoryEventHandler
static class SampleOrderEventHandler2 {
boolean wasCalled = false;
long timestamp;
@Order(1)
@HandleBeforeCreate
private void method(Person sample) {
wasCalled = true;
timestamp = System.nanoTime();
}
}
}