From 180c640f545aacb99d7aa6b00b2e7b2ae5bb213c Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 15 Feb 2021 15:57:30 +0100 Subject: [PATCH] Consider primary repository definition in Repositories when created Repositories using ApplicationContext. Closes #1583. Original pull request: #465. --- .../data/repository/support/Repositories.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/support/Repositories.java b/src/main/java/org/springframework/data/repository/support/Repositories.java index 606426d1c..fb1e4ac85 100644 --- a/src/main/java/org/springframework/data/repository/support/Repositories.java +++ b/src/main/java/org/springframework/data/repository/support/Repositories.java @@ -29,6 +29,7 @@ import org.springframework.beans.factory.BeanFactoryUtils; 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; @@ -276,10 +277,20 @@ public class Repositories implements Iterable> { if (repositoryBeanNames.containsKey(type)) { - Boolean presentAndPrimary = beanFactory // - .filter(ConfigurableListableBeanFactory.class::isInstance) // - .map(ConfigurableListableBeanFactory.class::cast) // - .map(it -> it.getBeanDefinition(name)) // + Optional factoryToUse = this.beanFactory.map(it -> { + + if (it instanceof ConfigurableListableBeanFactory) { + return (ConfigurableListableBeanFactory) it; + } + + if (it instanceof ConfigurableApplicationContext) { + return ((ConfigurableApplicationContext) it).getBeanFactory(); + } + + return null; + }); + + Boolean presentAndPrimary = factoryToUse.map(it -> it.getBeanDefinition(name)) // .map(BeanDefinition::isPrimary) // .orElse(false);