Skip to content

Commit f6630a5

Browse files
committed
DATACMNS-1663 - Polishing.
A few refactorings for more readable code in the publishing method interceptor. Tweaks to the reference documentation to include the CrudRepository methods that now also publish the events. Original pull request: #436.
1 parent 889ae01 commit f6630a5

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/main/asciidoc/repositories.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ class AnAggregateRoot {
10901090
<2> After all events have been published, we have a method annotated with `@AfterDomainEventPublication`. It can be used to potentially clean the list of events to be published (among other uses).
10911091
====
10921092

1093-
The methods are called every time one of a Spring Data repository's `save(…)` methods is called.
1093+
The methods are called every time one of a Spring Data repository's `save(…)`, `saveAll(…)`, `delete(…)` or `deleteAll(…)` methods are called.
10941094

10951095
[[core.extensions]]
10961096
== Spring Data Extensions

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

+23-15
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import org.aopalliance.intercept.MethodInterceptor;
2626
import org.aopalliance.intercept.MethodInvocation;
27-
2827
import org.springframework.aop.framework.ProxyFactory;
2928
import org.springframework.context.ApplicationEventPublisher;
3029
import org.springframework.data.domain.AfterDomainEventPublication;
@@ -41,9 +40,8 @@
4140
* {@link RepositoryProxyPostProcessor} to register a {@link MethodInterceptor} to intercept
4241
* {@link CrudRepository#save(Object)} and {@link CrudRepository#delete(Object)} methods and publish events potentially
4342
* exposed via a method annotated with {@link DomainEvents}. If no such method can be detected on the aggregate root, no
44-
* interceptor is added. Additionally,
45-
* the aggregate root can expose a method annotated with {@link AfterDomainEventPublication}. If present, the method
46-
* will be invoked after all events have been published.
43+
* interceptor is added. Additionally, the aggregate root can expose a method annotated with
44+
* {@link AfterDomainEventPublication}. If present, the method will be invoked after all events have been published.
4745
*
4846
* @author Oliver Gierke
4947
* @author Christoph Strobl
@@ -77,7 +75,8 @@ public void postProcess(ProxyFactory factory, RepositoryInformation repositoryIn
7775
}
7876

7977
/**
80-
* {@link MethodInterceptor} to publish events exposed an aggregate on calls to a save or delete method on the repository.
78+
* {@link MethodInterceptor} to publish events exposed an aggregate on calls to a save or delete method on the
79+
* repository.
8180
*
8281
* @author Oliver Gierke
8382
* @since 1.13
@@ -103,27 +102,36 @@ public static EventPublishingMethodInterceptor of(EventPublishingMethod eventMet
103102
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
104103
*/
105104
@Override
106-
public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {
105+
@Nullable
106+
public Object invoke(MethodInvocation invocation) throws Throwable {
107107

108-
Object[] arguments = invocation.getArguments();
109108
Object result = invocation.proceed();
110109

111-
String methodName = invocation.getMethod().getName();
112-
Object eventSource;
113-
if (methodName.startsWith("save")) {
114-
eventSource = arguments.length == 1 ? arguments[0] : result;
115-
} else if ((methodName.equals("delete") || methodName.equals("deleteAll")) && arguments.length == 1) {
116-
eventSource = arguments[0];
117-
} else {
110+
if (!isEventPublishingMethod(invocation.getMethod())) {
118111
return result;
119112
}
120113

121-
eventMethod.publishEventsFrom(eventSource, publisher);
114+
Object[] arguments = invocation.getArguments();
115+
116+
eventMethod.publishEventsFrom(arguments[0], publisher);
122117

123118
return result;
124119
}
125120
}
126121

122+
private static boolean isEventPublishingMethod(Method method) {
123+
return method.getParameterCount() == 1 //
124+
&& (isSaveMethod(method.getName()) || isDeleteMethod(method.getName()));
125+
}
126+
127+
private static boolean isSaveMethod(String methodName) {
128+
return methodName.startsWith("save");
129+
}
130+
131+
private static boolean isDeleteMethod(String methodName) {
132+
return methodName.equals("delete") || methodName.equals("deleteAll");
133+
}
134+
127135
/**
128136
* Abstraction of a method on the aggregate root that exposes the events to publish.
129137
*

0 commit comments

Comments
 (0)