Skip to content

DATACMNS-1142 - Consider @Primary repository in Repositories if context is given. #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.repository.core.EntityInformation;
Expand All @@ -46,6 +47,7 @@
* @author Thomas Darimont
* @author Thomas Eizinger
* @author Christoph Strobl
* @author Jan Zeppenfeld
*/
public class Repositories implements Iterable<Class<?>> {

Expand Down Expand Up @@ -276,9 +278,16 @@ private void cacheFirstOrPrimary(Class<?> type, RepositoryFactoryInformation inf

if (repositoryBeanNames.containsKey(type)) {

Boolean presentAndPrimary = beanFactory //
ConfigurableListableBeanFactory configurableListableBeanFactory = beanFactory
.filter(ConfigurableListableBeanFactory.class::isInstance) //
.map(ConfigurableListableBeanFactory.class::cast) //
.orElse(beanFactory //
.filter(ConfigurableApplicationContext.class::isInstance) //
.map(ConfigurableApplicationContext.class::cast) //
.map(ConfigurableApplicationContext::getBeanFactory) //
.orElse(null));

Boolean presentAndPrimary = Optional.ofNullable(configurableListableBeanFactory)
.map(it -> it.getBeanDefinition(name)) //
.map(BeanDefinition::isPrimary) //
.orElse(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Jan Zeppenfeld
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
Expand Down Expand Up @@ -183,6 +184,28 @@ void keepsPrimaryRepositoryInCaseOfMultipleOnes() {
});
}

@Test // DATAREST-923, DATACMNS-1142
void keepsPrimaryRepositoryInCaseOfMultipleOnesIfContextIsNotAConfigurableListableBeanFactory() {

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(context);

assertThat(repositories.getRepositoryFor(SomeEntity.class)).hasValueSatisfying(it -> {
assertThat(it).isInstanceOf(PrimaryRepository.class);
});
}

class Person {}

class Address {}
Expand Down