DATACMNS-1448 - Repositories now favors primary repositories.

If multiple repositories pointing to a single domain type are found we now favor the one registered as primary bean definition. This allows allows reliable disambiguation on which repository is supposed to be used for generic repository interactions.

Related tickets: DATAREST-923.
This commit is contained in:
Oliver Drotbohm
2018-12-12 22:27:50 +01:00
parent 97390a1eef
commit d8705c1cca
2 changed files with 65 additions and 4 deletions

View File

@@ -33,6 +33,7 @@ import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.context.SampleMappingContext;
@@ -158,6 +159,28 @@ public class RepositoriesUnitTests {
assertThat(repositories.getRepositoryFor(proxy.getClass())).isNotEmpty();
}
@Test // DATACMNS-1448
public void keepsPrimaryRepositoryInCaseOfMultipleOnes() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerBeanDefinition("first", getRepositoryBeanDefinition(FirstRepository.class));
AbstractBeanDefinition definition = getRepositoryBeanDefinition(PrimaryRepository.class);
definition.setPrimary(true);
beanFactory.registerBeanDefinition("primary", definition);
beanFactory.registerBeanDefinition("third", getRepositoryBeanDefinition(ThirdRepository.class));
context = new GenericApplicationContext(beanFactory);
context.refresh();
Repositories repositories = new Repositories(beanFactory);
assertThat(repositories.getRepositoryFor(SomeEntity.class)).hasValueSatisfying(it -> {
assertThat(it).isInstanceOf(PrimaryRepository.class);
});
}
class Person {}
class Address {}
@@ -244,4 +267,13 @@ public class RepositoriesUnitTests {
static class SampleEntity implements Sample {}
interface SampleRepository extends Repository<Sample, Long> {}
interface SomeEntity {}
interface FirstRepository extends Repository<SomeEntity, Long> {}
@Primary
interface PrimaryRepository extends Repository<SomeEntity, Long> {}
interface ThirdRepository extends Repository<SomeEntity, Long> {}
}