Browse Source

Set `ApplicationEventPublisher` on repository factory if it implements `ApplicationEventPublisherAware`.

Closes #3424
pull/3426/head
Mark Paluch 2 weeks ago
parent
commit
92df68b226
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 8
      src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.java
  2. 15
      src/test/java/org/springframework/data/repository/core/support/DummyRepositoryFactory.java
  3. 23
      src/test/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupportUnitTests.java

8
src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.java

@ -331,19 +331,23 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>, @@ -331,19 +331,23 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>,
: 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);
}

15
src/test/java/org/springframework/data/repository/core/support/DummyRepositoryFactory.java

@ -25,6 +25,8 @@ import org.mockito.ArgumentMatchers; @@ -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; @@ -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 { @@ -54,6 +56,8 @@ public class DummyRepositoryFactory extends RepositoryFactorySupport {
private final ApplicationStartup applicationStartup;
private ApplicationEventPublisher publisher;
@SuppressWarnings("unchecked") private final QuerydslPredicateExecutor<Object> querydsl = mock(
QuerydslPredicateExecutor.class);
private final Object repository;
@ -97,6 +101,15 @@ public class DummyRepositoryFactory extends RepositoryFactorySupport { @@ -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) {

23
src/test/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupportUnitTests.java

@ -23,6 +23,7 @@ import org.aopalliance.intercept.MethodInvocation; @@ -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; @@ -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 { @@ -97,7 +99,7 @@ class RepositoryFactoryBeanSupportUnitTests {
@Test // DATACMNS-1345
void reportsMappingContextUnavailableForPersistentEntityLookup() {
var bean = new RepositoryFactoryBeanSupport<SampleRepository, Object, Long>(
var bean = new RepositoryFactoryBeanSupport<>(
SampleRepository.class) {
@Override
@ -112,6 +114,25 @@ class RepositoryFactoryBeanSupportUnitTests { @@ -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<Object, Long> {
void someMethod();

Loading…
Cancel
Save