diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadata.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadata.java index 44c6c97cec..ee1d1fcd46 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadata.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadata.java @@ -15,7 +15,6 @@ */ package org.springframework.data.mongodb.repository.support; -import java.lang.reflect.Method; import java.util.Optional; import com.mongodb.ReadPreference; @@ -25,6 +24,7 @@ * execution. * * @author Mark Paluch + * @author Christoph Strobl * @since 4.2 */ public interface CrudMethodMetadata { @@ -36,10 +36,4 @@ public interface CrudMethodMetadata { */ Optional getReadPreference(); - /** - * Returns the {@link Method} that this metadata applies to. - * - * @return the {@link Method} that this metadata applies to. - */ - Method getMethod(); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java index 1c4cb75bc1..22992b7335 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java @@ -45,6 +45,8 @@ * information or query hints on them. * * @author Mark Paluch + * @author Christoph Strobl + * @since 4.2 */ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, BeanClassLoaderAware { @@ -103,13 +105,15 @@ static class CrudMethodMetadataPopulatingMethodInterceptor implements MethodInte */ static MethodInvocation currentInvocation() throws IllegalStateException { - MethodInvocation mi = currentInvocation.get(); + MethodInvocation invocation = currentInvocation.get(); - if (mi == null) - throw new IllegalStateException( - "No MethodInvocation found: Check that an AOP invocation is in progress, and that the " - + "CrudMethodMetadataPopulatingMethodInterceptor is upfront in the interceptor chain."); - return mi; + if (invocation != null) { + return invocation; + } + + throw new IllegalStateException( + "No MethodInvocation found: Check that an AOP invocation is in progress, and that the " + + "CrudMethodMetadataPopulatingMethodInterceptor is upfront in the interceptor chain."); } @Override @@ -163,7 +167,6 @@ public Object invoke(MethodInvocation invocation) throws Throwable { static class DefaultCrudMethodMetadata implements CrudMethodMetadata { private final Optional readPreference; - private final Method method; /** * Creates a new {@link DefaultCrudMethodMetadata} for the given {@link Method}. @@ -175,7 +178,6 @@ static class DefaultCrudMethodMetadata implements CrudMethodMetadata { Assert.notNull(method, "Method must not be null"); this.readPreference = findReadPreference(method); - this.method = method; } private Optional findReadPreference(Method method) { @@ -201,11 +203,6 @@ private Optional findReadPreference(Method method) { public Optional getReadPreference() { return readPreference; } - - @Override - public Method getMethod() { - return method; - } } private static class ThreadBoundTargetSource implements TargetSource { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java index 5671801d78..dd7a38fd1b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java @@ -357,8 +357,9 @@ public R findBy(Example example, * applied to queries. * * @param crudMethodMetadata + * @since 4.2 */ - public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) { + void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) { this.crudMethodMetadata = crudMethodMetadata; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java index e5361a34de..1c84e5fde3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java @@ -434,8 +434,9 @@ public > P findBy(Example example, * applied to queries. * * @param crudMethodMetadata + * @since 4.2 */ - public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) { + void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) { this.crudMethodMetadata = crudMethodMetadata; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java index 8929f5c4f0..09ab6090b8 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java @@ -20,6 +20,7 @@ import java.io.Serializable; import java.util.Optional; +import java.util.Set; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -41,6 +42,7 @@ import org.springframework.data.mongodb.repository.Person; import org.springframework.data.mongodb.repository.ReadPreference; import org.springframework.data.mongodb.repository.query.MongoEntityInformation; +import org.springframework.data.repository.ListCrudRepository; import org.springframework.data.repository.Repository; /** @@ -48,6 +50,7 @@ * * @author Oliver Gierke * @author Mark Paluch + * @author Christoph Strobl */ @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) @@ -92,7 +95,21 @@ void considersCrudMethodMetadata() { assertThat(value.getReadPreference()).isEqualTo(com.mongodb.ReadPreference.secondary()); } - interface MyPersonRepository extends Repository { + @Test // GH-2971 + void ignoresCrudMethodMetadataOnNonAnnotatedMethods() { + + MongoRepositoryFactory factory = new MongoRepositoryFactory(template); + MyPersonRepository repository = factory.getRepository(MyPersonRepository.class); + repository.findAllById(Set.of(42L)); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Query.class); + verify(template).find(captor.capture(), eq(Person.class), eq("person")); + + Query value = captor.getValue(); + assertThat(value.getReadPreference()).isNull(); + } + + interface MyPersonRepository extends ListCrudRepository { @ReadPreference("secondary") Optional findById(Long id); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactoryUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactoryUnitTests.java index 49a5513242..ea54b00f6b 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactoryUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactoryUnitTests.java @@ -18,6 +18,10 @@ import static org.assertj.core.api.Assertions.*; import static org.mockito.Mockito.*; +import java.util.Set; + +import org.springframework.data.repository.reactive.ReactiveCrudRepository; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import org.junit.jupiter.api.BeforeEach; @@ -72,7 +76,23 @@ void considersCrudMethodMetadata() { assertThat(value.getReadPreference()).isEqualTo(com.mongodb.ReadPreference.secondary()); } - interface MyPersonRepository extends Repository { + @Test // GH-2971 + void ignoresCrudMethodMetadataOnNonAnnotatedMethods() { + + when(template.find(any(), any(), anyString())).thenReturn(Flux.empty()); + + ReactiveMongoRepositoryFactory factory = new ReactiveMongoRepositoryFactory(template); + MyPersonRepository repository = factory.getRepository(MyPersonRepository.class); + repository.findAllById(Set.of(42L)); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Query.class); + verify(template).find(captor.capture(), eq(Person.class), eq("person")); + + Query value = captor.getValue(); + assertThat(value.getReadPreference()).isNull(); + } + + interface MyPersonRepository extends ReactiveCrudRepository { @ReadPreference("secondary") Mono findById(Long id); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepositoryUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepositoryUnitTests.java index 32462a62c3..7e3751dac1 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepositoryUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepositoryUnitTests.java @@ -161,11 +161,6 @@ void shouldAddReadPreferenceToFindAllMethods( public Optional getReadPreference() { return Optional.of(com.mongodb.ReadPreference.secondaryPreferred()); } - - @Override - public Method getMethod() { - return null; - } }); when(mongoOperations.find(any(), any(), any())).thenReturn(Flux.just("ok")); @@ -186,11 +181,6 @@ void shouldAddReadPreferenceToFindOne() { public Optional getReadPreference() { return Optional.of(com.mongodb.ReadPreference.secondaryPreferred()); } - - @Override - public Method getMethod() { - return null; - } }); when(mongoOperations.find(any(), any(), any())).thenReturn(Flux.just("ok")); @@ -218,11 +208,6 @@ void shouldAddReadPreferenceToFluentFetchable() { public Optional getReadPreference() { return Optional.of(com.mongodb.ReadPreference.secondaryPreferred()); } - - @Override - public Method getMethod() { - return null; - } }); repository.findBy(Example.of(new TestDummy()), FluentQuery.ReactiveFluentQuery::all).subscribe();