From 92df68b2268aa713a196c5f2d213efb9e4161310 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 4 Dec 2025 16:02:51 +0100 Subject: [PATCH] Set `ApplicationEventPublisher` on repository factory if it implements `ApplicationEventPublisherAware`. Closes #3424 --- .../support/RepositoryFactoryBeanSupport.java | 8 +++++-- .../core/support/DummyRepositoryFactory.java | 15 +++++++++++- ...RepositoryFactoryBeanSupportUnitTests.java | 23 ++++++++++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.java b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.java index 2f505148a..49635ae66 100644 --- a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.java +++ b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.java @@ -331,19 +331,23 @@ public abstract class RepositoryFactoryBeanSupport, : QueryMethodValueEvaluationContextAccessor.DEFAULT_CONTEXT_PROVIDER); this.factory.setBeanClassLoader(classLoader); - if (beanFactory != null) { + if (this.beanFactory != null) { this.factory.setBeanFactory(beanFactory); } if (this.publisher != null) { this.factory.addRepositoryProxyPostProcessor(new EventPublishingRepositoryProxyPostProcessor(publisher)); + + if (this.factory instanceof ApplicationEventPublisherAware aware) { + aware.setApplicationEventPublisher(this.publisher); + } } if (this.environment != null) { this.factory.setEnvironment(this.environment); } - if (repositoryBaseClass != null) { + if (this.repositoryBaseClass != null) { this.factory.setRepositoryBaseClass(repositoryBaseClass); } diff --git a/src/test/java/org/springframework/data/repository/core/support/DummyRepositoryFactory.java b/src/test/java/org/springframework/data/repository/core/support/DummyRepositoryFactory.java index dd7a51886..85f26fe8b 100644 --- a/src/test/java/org/springframework/data/repository/core/support/DummyRepositoryFactory.java +++ b/src/test/java/org/springframework/data/repository/core/support/DummyRepositoryFactory.java @@ -25,6 +25,8 @@ import org.mockito.ArgumentMatchers; import org.mockito.Mockito; import org.springframework.beans.factory.BeanFactory; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.core.metrics.ApplicationStartup; import org.springframework.core.metrics.StartupStep; import org.springframework.data.projection.ProjectionFactory; @@ -46,7 +48,7 @@ import org.springframework.data.repository.query.ValueExpressionDelegate; * @author Oliver Gierke * @author Christoph Strobl */ -public class DummyRepositoryFactory extends RepositoryFactorySupport { +public class DummyRepositoryFactory extends RepositoryFactorySupport implements ApplicationEventPublisherAware { public final MyRepositoryQuery queryOne = mock(MyRepositoryQuery.class); public final RepositoryQuery queryTwo = mock(RepositoryQuery.class); @@ -54,6 +56,8 @@ public class DummyRepositoryFactory extends RepositoryFactorySupport { private final ApplicationStartup applicationStartup; + private ApplicationEventPublisher publisher; + @SuppressWarnings("unchecked") private final QuerydslPredicateExecutor querydsl = mock( QuerydslPredicateExecutor.class); private final Object repository; @@ -97,6 +101,15 @@ public class DummyRepositoryFactory extends RepositoryFactorySupport { return Optional.of(strategy); } + public ApplicationEventPublisher getPublisher() { + return publisher; + } + + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { + this.publisher = publisher; + } + @Override protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) { diff --git a/src/test/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupportUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupportUnitTests.java index d5e43c815..b3380cf44 100755 --- a/src/test/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupportUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupportUnitTests.java @@ -23,6 +23,7 @@ import org.aopalliance.intercept.MethodInvocation; import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.Test; + import org.springframework.data.querydsl.QuerydslPredicateExecutor; import org.springframework.data.repository.Repository; import org.springframework.test.util.ReflectionTestUtils; @@ -32,6 +33,7 @@ import org.springframework.test.util.ReflectionTestUtils; * * @author Oliver Gierke * @author Thomas Darimont + * @author Mark Paluch */ class RepositoryFactoryBeanSupportUnitTests { @@ -97,7 +99,7 @@ class RepositoryFactoryBeanSupportUnitTests { @Test // DATACMNS-1345 void reportsMappingContextUnavailableForPersistentEntityLookup() { - var bean = new RepositoryFactoryBeanSupport( + var bean = new RepositoryFactoryBeanSupport<>( SampleRepository.class) { @Override @@ -112,6 +114,25 @@ class RepositoryFactoryBeanSupportUnitTests { .isThrownBy(() -> bean.getPersistentEntity()); } + @Test // DATACMNS-1345 + void setsApplicationEventPublisher() { + + var bean = new RepositoryFactoryBeanSupport<>(SampleRepository.class) { + + @Override + protected RepositoryFactorySupport createRepositoryFactory() { + return new DummyRepositoryFactory(mock(SampleRepository.class)); + } + }; + + bean.setApplicationEventPublisher(event -> {}); + bean.setLazyInit(true); + bean.afterPropertiesSet(); + + var factory = ReflectionTestUtils.getField(bean, "factory"); + assertThat(factory).extracting("publisher").isNotNull(); + } + interface SampleRepository extends Repository { void someMethod();