Skip to content

Commit 5d08bd6

Browse files
committed
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….
1 parent 044a0c0 commit 5d08bd6

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/main/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Collection;
2222
import java.util.Collections;
2323
import java.util.Map;
24-
import java.util.stream.Stream;
2524

2625
import org.aopalliance.intercept.MethodInterceptor;
2726
import org.aopalliance.intercept.MethodInvocation;
@@ -90,7 +89,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
9089

9190
Object result = invocation.proceed();
9291

93-
if (!Stream.of("save", "saveAll").anyMatch(it -> invocation.getMethod().getName().equals(it))) {
92+
if (!invocation.getMethod().getName().startsWith("save")) {
9493
return result;
9594
}
9695

src/test/java/org/springframework/data/repository/core/support/EventPublishingRepositoryProxyPostProcessorUnitTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import lombok.Getter;
2323
import lombok.Value;
2424

25+
import java.lang.reflect.Method;
2526
import java.util.Arrays;
2627
import java.util.Collection;
2728
import java.util.Collections;
@@ -187,6 +188,23 @@ public void publishesEventsAfterSaveInvocation() throws Throwable {
187188
}
188189
}
189190

191+
@Test // DATACMNS-1113
192+
public void invokesEventsForMethodsThatStartsWithSave() throws Throwable {
193+
194+
Method method = SampleRepository.class.getMethod("saveAndFlush", MultipleEvents.class);
195+
doReturn(method).when(invocation).getMethod();
196+
197+
SomeEvent event = new SomeEvent();
198+
MultipleEvents sample = MultipleEvents.of(Collections.singletonList(event));
199+
doReturn(new Object[] { sample }).when(invocation).getArguments();
200+
201+
EventPublishingMethodInterceptor//
202+
.of(EventPublishingMethod.of(MultipleEvents.class), publisher)//
203+
.invoke(invocation);
204+
205+
verify(publisher).publishEvent(event);
206+
}
207+
190208
@Value(staticConstructor = "of")
191209
static class MultipleEvents {
192210
@Getter(onMethod = @__(@DomainEvents)) Collection<? extends Object> events;
@@ -202,5 +220,8 @@ static class SomeEvent {
202220
UUID id = UUID.randomUUID();
203221
}
204222

205-
interface SampleRepository extends CrudRepository<MultipleEvents, Long> {}
223+
interface SampleRepository extends CrudRepository<MultipleEvents, Long> {
224+
225+
MultipleEvents saveAndFlush(MultipleEvents events);
226+
}
206227
}

0 commit comments

Comments
 (0)