DATACMNS-1113 - Domain event publication now happens for all methods starting with save….

Previously we explicitly intercepted repository methods named save(…) and saveAll(…) which unfortunately results in custom variants of that (e.g. JpaRepository's saveAndFlush(…)) not causing event publication.

We now publish events for methods whose names start with save….
This commit is contained in:
Oliver Gierke
2017-05-11 09:20:36 +02:00
parent e6b955bf82
commit e8e3b03d24
2 changed files with 24 additions and 2 deletions

View File

@@ -89,7 +89,7 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
Object result = invocation.proceed();
if (!invocation.getMethod().getName().equals("save")) {
if (!invocation.getMethod().getName().startsWith("save")) {
return result;
}

View File

@@ -25,8 +25,10 @@ import lombok.Getter;
import lombok.Value;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.UUID;
import org.aopalliance.aop.Advice;
@@ -191,6 +193,23 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests {
}
}
@Test // DATACMNS-1113
public void invokesEventsForMethodsThatStartsWithSave() throws Throwable {
Method method = SampleRepository.class.getMethod("saveAndFlush", MultipleEvents.class);
doReturn(method).when(invocation).getMethod();
SomeEvent event = new SomeEvent();
MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event));
doReturn(new Object[] { sample }).when(invocation).getArguments();
EventPublishingMethodInterceptor//
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
.invoke(invocation);
verify(publisher).publishEvent(event);
}
@Value(staticConstructor = "of")
static class MultipleEvents {
@Getter(onMethod = @__(@DomainEvents)) Collection<? extends Object> events;
@@ -206,5 +225,8 @@ public class EventPublishingRepositoryProxyPostProcessorUnitTests {
UUID id = UUID.randomUUID();
}
interface SampleRepository extends CrudRepository<MultipleEvents, Long> {}
interface SampleRepository extends CrudRepository<MultipleEvents, Long> {
MultipleEvents saveAndFlush(MultipleEvents events);
}
}