Skip to content

Commit a9a4098

Browse files
committed
Register domain event handling methods on AbstractAggregateRoot for native reflection.
1 parent cd11e02 commit a9a4098

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/main/java/org/springframework/data/repository/aot/hint/RepositoryRuntimeHints.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,28 @@
1515
*/
1616
package org.springframework.data.repository.aot.hint;
1717

18+
import java.lang.annotation.Annotation;
1819
import java.util.Arrays;
20+
import java.util.Collection;
21+
import java.util.List;
1922
import java.util.Properties;
2023

2124
import org.jspecify.annotations.Nullable;
2225

2326
import org.springframework.aop.SpringProxy;
2427
import org.springframework.aop.framework.Advised;
28+
import org.springframework.aot.hint.ExecutableMode;
2529
import org.springframework.aot.hint.MemberCategory;
2630
import org.springframework.aot.hint.RuntimeHints;
2731
import org.springframework.aot.hint.RuntimeHintsRegistrar;
2832
import org.springframework.aot.hint.TypeReference;
2933
import org.springframework.beans.factory.BeanFactory;
3034
import org.springframework.core.DecoratingProxy;
35+
import org.springframework.core.annotation.AnnotatedElementUtils;
3136
import org.springframework.core.io.InputStreamSource;
37+
import org.springframework.data.domain.AbstractAggregateRoot;
38+
import org.springframework.data.domain.AfterDomainEventPublication;
39+
import org.springframework.data.domain.DomainEvents;
3240
import org.springframework.data.domain.Example;
3341
import org.springframework.data.mapping.context.MappingContext;
3442
import org.springframework.data.repository.core.RepositoryMetadata;
@@ -42,17 +50,22 @@
4250
import org.springframework.data.repository.query.QueryByExampleExecutor;
4351
import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor;
4452
import org.springframework.data.util.ReactiveWrappers;
53+
import org.springframework.util.ReflectionUtils;
4554

4655
/**
4756
* {@link RuntimeHintsRegistrar} holding required hints to bootstrap data repositories. <br />
4857
* Already registered via {@literal aot.factories}.
4958
*
5059
* @author Christoph Strobl
5160
* @author Mark Paluch
61+
* @author Oliver Drotbohm
5262
* @since 3.0
5363
*/
5464
class RepositoryRuntimeHints implements RuntimeHintsRegistrar {
5565

66+
private static final Collection<Class<? extends Annotation>> DOMAIN_EVENT_ANNOTATIONS = List
67+
.of(AfterDomainEventPublication.class, DomainEvents.class);
68+
5669
@Override
5770
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
5871

@@ -72,6 +85,17 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader)
7285
builder.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS);
7386
});
7487

88+
// Register event handling methods on AbstractAggregateRoot for reflection
89+
ReflectionUtils.doWithLocalMethods(AbstractAggregateRoot.class, method -> {
90+
91+
DOMAIN_EVENT_ANNOTATIONS.forEach(it -> {
92+
93+
if (AnnotatedElementUtils.isAnnotated(method, it)) {
94+
hints.reflection().registerMethod(method, ExecutableMode.INVOKE);
95+
}
96+
});
97+
});
98+
7599
if (ReactiveWrappers.PROJECT_REACTOR_PRESENT) {
76100

77101
// repository infrastructure

0 commit comments

Comments
 (0)