Skip to content

Commit 8fc0159

Browse files
committed
DATACMNS-836 - Improve reactive repository configuration.
RepositoryConfigurationExtensionSupport now exposes a ….useRepositoryConfiguration(RepositoryMetadata) so that extensions can opt in or out of an interface taking part in configuration more easily. Turned loadRepositoryInterface(…) private again as extensions should be able to just inspect the RepositoryMetadata now. Some parameter rename in ReactiveWrapperConverters to avoid confusion.
1 parent 7ea8c9d commit 8fc0159

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

src/main/java/org/springframework/data/repository/config/RepositoryConfigurationExtensionSupport.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,25 @@ public <T extends RepositoryConfigurationSource> Collection<RepositoryConfigurat
8787
for (BeanDefinition candidate : configSource.getCandidates(loader)) {
8888

8989
RepositoryConfiguration<T> configuration = getRepositoryConfiguration(candidate, configSource);
90+
Class<?> repositoryInterface = loadRepositoryInterface(configuration, loader);
9091

91-
if (!strictMatchesOnly || configSource.usesExplicitFilters()) {
92+
if (repositoryInterface == null) {
9293
result.add(configuration);
9394
continue;
9495
}
9596

96-
Class<?> repositoryInterface = loadRepositoryInterface(configuration, loader);
97+
RepositoryMetadata metadata = AbstractRepositoryMetadata.getMetadata(repositoryInterface);
98+
99+
if (!useRepositoryConfiguration(metadata)) {
100+
continue;
101+
}
102+
103+
if (!strictMatchesOnly || configSource.usesExplicitFilters()) {
104+
result.add(configuration);
105+
continue;
106+
}
97107

98-
if (repositoryInterface == null || isStrictRepositoryCandidate(repositoryInterface)) {
108+
if (isStrictRepositoryCandidate(metadata)) {
99109
result.add(configuration);
100110
}
101111
}
@@ -246,7 +256,7 @@ protected <T extends RepositoryConfigurationSource> RepositoryConfiguration<T> g
246256
}
247257

248258
/**
249-
* Returns whether the given repository interface is a candidate for bean definition creation in the strict repository
259+
* Returns whether the given repository metadata is a candidate for bean definition creation in the strict repository
250260
* detection mode. The default implementation inspects the domain type managed for a set of well-known annotations
251261
* (see {@link #getIdentifyingAnnotations()}). If none of them is found, the candidate is discarded. Implementations
252262
* should make sure, the only return {@literal true} if they're really sure the interface handed to the method is
@@ -256,11 +266,10 @@ protected <T extends RepositoryConfigurationSource> RepositoryConfiguration<T> g
256266
* @return
257267
* @since 1.9
258268
*/
259-
protected boolean isStrictRepositoryCandidate(Class<?> repositoryInterface) {
260-
261-
RepositoryMetadata metadata = AbstractRepositoryMetadata.getMetadata(repositoryInterface);
269+
protected boolean isStrictRepositoryCandidate(RepositoryMetadata metadata) {
262270

263271
Collection<Class<?>> types = getIdentifyingTypes();
272+
Class<?> repositoryInterface = metadata.getRepositoryInterface();
264273

265274
for (Class<?> type : types) {
266275
if (type.isAssignableFrom(repositoryInterface)) {
@@ -286,6 +295,16 @@ protected boolean isStrictRepositoryCandidate(Class<?> repositoryInterface) {
286295
return false;
287296
}
288297

298+
/**
299+
* Return whether to use the configuration for the repository with the given metadata. Defaults to {@literal true}.
300+
*
301+
* @param metadata will never be {@literal null}.
302+
* @return
303+
*/
304+
protected boolean useRepositoryConfiguration(RepositoryMetadata metadata) {
305+
return true;
306+
}
307+
289308
/**
290309
* Loads the repository interface contained in the given {@link RepositoryConfiguration} using the given
291310
* {@link ResourceLoader}.
@@ -294,7 +313,7 @@ protected boolean isStrictRepositoryCandidate(Class<?> repositoryInterface) {
294313
* @param loader must not be {@literal null}.
295314
* @return the repository interface or {@literal null} if it can't be loaded.
296315
*/
297-
protected Class<?> loadRepositoryInterface(RepositoryConfiguration<?> configuration, ResourceLoader loader) {
316+
private Class<?> loadRepositoryInterface(RepositoryConfiguration<?> configuration, ResourceLoader loader) {
298317

299318
String repositoryInterface = configuration.getRepositoryInterface();
300319
ClassLoader classLoader = loader.getClassLoader();

src/main/java/org/springframework/data/repository/util/ReactiveWrapperConverters.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,21 @@ public static <T> T toWrapper(Object stream, Class<? extends T> expectedWrapperT
193193
/**
194194
* Maps elements of a reactive element stream to other elements.
195195
*
196-
* @param stream must not be {@literal null}.
196+
* @param reactiveObject must not be {@literal null}.
197197
* @param converter must not be {@literal null}.
198198
* @return
199199
*/
200200
@SuppressWarnings("unchecked")
201-
public static <T> T map(Object stream, Function<Object, Object> converter) {
201+
public static <T> T map(Object reactiveObject, Function<Object, Object> converter) {
202202

203-
Assert.notNull(stream, "Stream must not be null!");
203+
Assert.notNull(reactiveObject, "Reactive source object must not be null!");
204204
Assert.notNull(converter, "Converter must not be null!");
205205

206206
return REACTIVE_WRAPPERS.stream()//
207-
.filter(it -> ClassUtils.isAssignable(it.getWrapperClass(), stream.getClass()))//
207+
.filter(it -> ClassUtils.isAssignable(it.getWrapperClass(), reactiveObject.getClass()))//
208208
.findFirst()//
209-
.map(it -> (T) it.map(stream, converter))//
210-
.orElseThrow(() -> new IllegalStateException(String.format("Cannot apply converter to %s", stream)));
209+
.map(it -> (T) it.map(reactiveObject, converter))//
210+
.orElseThrow(() -> new IllegalStateException(String.format("Cannot apply converter to %s", reactiveObject)));
211211
}
212212

213213
// -------------------------------------------------------------------------

src/test/java/org/springframework/data/repository/config/RepositoryConfigurationExtensionSupportUnitTests.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.springframework.core.type.AnnotationMetadata;
3131
import org.springframework.core.type.StandardAnnotationMetadata;
3232
import org.springframework.data.repository.Repository;
33+
import org.springframework.data.repository.core.RepositoryMetadata;
34+
import org.springframework.data.repository.core.support.AbstractRepositoryMetadata;
3335
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
3436

3537
/**
@@ -46,23 +48,29 @@ public class RepositoryConfigurationExtensionSupportUnitTests {
4648
*/
4749
@Test
4850
public void doesNotConsiderRepositoryForPlainTypeStrictMatch() {
49-
assertThat(extension.isStrictRepositoryCandidate(PlainTypeRepository.class), is(false));
51+
52+
RepositoryMetadata metadata = AbstractRepositoryMetadata.getMetadata(PlainTypeRepository.class);
53+
assertThat(extension.isStrictRepositoryCandidate(metadata), is(false));
5054
}
5155

5256
/**
5357
* @see DATACMNS-526
5458
*/
5559
@Test
5660
public void considersRepositoryWithAnnotatedTypeStrictMatch() {
57-
assertThat(extension.isStrictRepositoryCandidate(AnnotatedTypeRepository.class), is(true));
61+
62+
RepositoryMetadata metadata = AbstractRepositoryMetadata.getMetadata(AnnotatedTypeRepository.class);
63+
assertThat(extension.isStrictRepositoryCandidate(metadata), is(true));
5864
}
5965

6066
/**
6167
* @see DATACMNS-526
6268
*/
6369
@Test
6470
public void considersRepositoryInterfaceExtendingStoreInterfaceStrictMatch() {
65-
assertThat(extension.isStrictRepositoryCandidate(ExtendingInterface.class), is(true));
71+
72+
RepositoryMetadata metadata = AbstractRepositoryMetadata.getMetadata(ExtendingInterface.class);
73+
assertThat(extension.isStrictRepositoryCandidate(metadata), is(true));
6674
}
6775

6876
/**
@@ -99,12 +107,12 @@ public String getRepositoryFactoryClassName() {
99107

100108
@Override
101109
protected Collection<Class<? extends Annotation>> getIdentifyingAnnotations() {
102-
return Collections.<Class<? extends Annotation>> singleton(Primary.class);
110+
return Collections.<Class<? extends Annotation>>singleton(Primary.class);
103111
}
104112

105113
@Override
106114
protected Collection<Class<?>> getIdentifyingTypes() {
107-
return Collections.<Class<?>> singleton(StoreInterface.class);
115+
return Collections.<Class<?>>singleton(StoreInterface.class);
108116
}
109117
}
110118

0 commit comments

Comments
 (0)