Skip to content

Commit f948a7f

Browse files
committed
DATACMNS-1596 - Modules not exposing identifying annotations do not claim repository interfaces anymore.
Previously, a module not exposing any entity identifying annotations would have claimed a repository definition and potentially overrode the bean definition of another store that was the proper claim in the first place. We have now changed this to abstain from a claim of the interface. We' also tweaked the log output in the following cases: 1. If the module neither returns an identifying type nor entity identifying annotations, we now log a warning that a module does not support a multi-module setup and answer all assignment requests with false. 2. The info level warning indicating an interface has been dropped now reports which annotations or interface base types to use. Related tickets: spring-projects/spring-boot#18721
1 parent 4eff423 commit f948a7f

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

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

+43-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.HashSet;
2424
import java.util.Set;
2525
import java.util.function.Supplier;
26+
import java.util.stream.Collectors;
2627

2728
import javax.annotation.Nullable;
2829

@@ -53,7 +54,9 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit
5354

5455
private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryConfigurationExtensionSupport.class);
5556
private static final String CLASS_LOADING_ERROR = "%s - Could not load type %s using class loader %s.";
56-
private static final String MULTI_STORE_DROPPED = "Spring Data {} - Could not safely identify store assignment for repository candidate {}.";
57+
private static final String MULTI_STORE_DROPPED = "Spring Data %s - Could not safely identify store assignment for repository candidate %s. If you want this repository to be a %s repository,";
58+
59+
private boolean noMultiStoreSupport = false;
5760

5861
/*
5962
* (non-Javadoc)
@@ -294,7 +297,22 @@ protected <T extends RepositoryConfigurationSource> RepositoryConfiguration<T> g
294297
*/
295298
protected boolean isStrictRepositoryCandidate(RepositoryMetadata metadata) {
296299

300+
if (noMultiStoreSupport) {
301+
return false;
302+
}
303+
297304
Collection<Class<?>> types = getIdentifyingTypes();
305+
Collection<Class<? extends Annotation>> annotations = getIdentifyingAnnotations();
306+
String moduleName = getModuleName();
307+
308+
if (types.isEmpty() && annotations.isEmpty()) {
309+
if (!noMultiStoreSupport) {
310+
LOGGER.warn("Spring Data {} does not support multi-store setups!", moduleName);
311+
noMultiStoreSupport = true;
312+
return false;
313+
}
314+
}
315+
298316
Class<?> repositoryInterface = metadata.getRepositoryInterface();
299317

300318
for (Class<?> type : types) {
@@ -304,19 +322,30 @@ protected boolean isStrictRepositoryCandidate(RepositoryMetadata metadata) {
304322
}
305323

306324
Class<?> domainType = metadata.getDomainType();
307-
Collection<Class<? extends Annotation>> annotations = getIdentifyingAnnotations();
308-
309-
if (annotations.isEmpty()) {
310-
return true;
311-
}
312325

313326
for (Class<? extends Annotation> annotationType : annotations) {
314327
if (AnnotationUtils.findAnnotation(domainType, annotationType) != null) {
315328
return true;
316329
}
317330
}
318331

319-
LOGGER.info(MULTI_STORE_DROPPED, getModuleName(), repositoryInterface);
332+
String message = String.format(MULTI_STORE_DROPPED, moduleName, repositoryInterface, moduleName);
333+
334+
if (!annotations.isEmpty()) {
335+
message = message.concat(" consider annotating your entities with one of these annotations: ") //
336+
.concat(toString(annotations)) //
337+
.concat(types.isEmpty() ? "." : " (preferred)");
338+
}
339+
340+
if (!types.isEmpty()) {
341+
342+
message = message.concat(annotations.isEmpty() ? " consider" : ", or consider") //
343+
.concat(" extending one of the following types with your repository: ") //
344+
.concat(toString(types)) //
345+
.concat(".");
346+
}
347+
348+
LOGGER.info(message);
320349

321350
return false;
322351
}
@@ -364,4 +393,11 @@ private Class<?> loadRepositoryInterface(RepositoryConfiguration<?> configuratio
364393

365394
return null;
366395
}
396+
397+
private static String toString(Collection<? extends Class<?>> types) {
398+
399+
return types.stream() //
400+
.map(Class::getName) //
401+
.collect(Collectors.joining(", "));
402+
}
367403
}

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

+29
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@
3434
import org.springframework.core.type.AnnotationMetadata;
3535
import org.springframework.core.type.StandardAnnotationMetadata;
3636
import org.springframework.dao.InvalidDataAccessApiUsageException;
37+
import org.springframework.data.repository.CrudRepository;
3738
import org.springframework.data.repository.Repository;
3839
import org.springframework.data.repository.core.RepositoryMetadata;
3940
import org.springframework.data.repository.core.support.AbstractRepositoryMetadata;
41+
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
4042
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
4143
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
4244

@@ -86,6 +88,15 @@ public void rejectsReactiveRepositories() {
8688
.hasMessageContaining("Reactive Repositories are not supported");
8789
}
8890

91+
@Test // DATACMNS-1596
92+
public void doesNotClaimEntityIfNoIdentifyingAnnotationsAreExposed() {
93+
94+
NonIdentifyingConfigurationExtension extension = new NonIdentifyingConfigurationExtension();
95+
RepositoryMetadata metadata = AbstractRepositoryMetadata.getMetadata(AnnotatedTypeRepository.class);
96+
97+
assertThat(extension.isStrictRepositoryCandidate(metadata)).isFalse();
98+
}
99+
89100
static class SampleRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
90101

91102
@Override
@@ -109,6 +120,24 @@ protected Collection<Class<?>> getIdentifyingTypes() {
109120
}
110121
}
111122

123+
static class NonIdentifyingConfigurationExtension extends RepositoryConfigurationExtensionSupport {
124+
125+
@Override
126+
protected String getModulePrefix() {
127+
return "non-identifying";
128+
}
129+
130+
@Override
131+
public String getRepositoryFactoryBeanClassName() {
132+
return RepositoryFactoryBeanSupport.class.getName();
133+
}
134+
135+
@Override
136+
protected Collection<Class<?>> getIdentifyingTypes() {
137+
return Collections.singleton(CrudRepository.class);
138+
}
139+
}
140+
112141
@Primary
113142
static class AnnotatedType {}
114143

0 commit comments

Comments
 (0)