diff --git a/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java b/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java index e4d493bd2..e26c1f3bc 100644 --- a/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java +++ b/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java @@ -91,6 +91,7 @@ public class DomainClassConverter { 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 c5e242a3e..176b8ca17 100644 --- a/src/main/java/org/springframework/data/repository/support/Repositories.java +++ b/src/main/java/org/springframework/data/repository/support/Repositories.java @@ -95,14 +95,14 @@ public class Repositories implements Iterable> { for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, RepositoryFactoryInformation.class, false, false)) { - cacheRepositoryFactory(name); + cacheRepositoryFactory(factory, name); } } @SuppressWarnings("rawtypes") - private synchronized void cacheRepositoryFactory(String name) { + private void cacheRepositoryFactory(ListableBeanFactory factory, String name) { - RepositoryFactoryInformation repositoryFactoryInformation = beanFactory.get().getBean(name, + RepositoryFactoryInformation repositoryFactoryInformation = factory.getBean(name, RepositoryFactoryInformation.class); RepositoryInformation information = repositoryFactoryInformation.getRepositoryInformation(); Class domainType = ClassUtils.getUserClass(information.getDomainType()); @@ -113,8 +113,21 @@ public class Repositories implements Iterable> { typesToRegister.add(domainType); typesToRegister.addAll(alternativeDomainTypes); + Optional beanFactory = Optional.of(factory).map(it -> { + + if (it instanceof ConfigurableListableBeanFactory) { + return (ConfigurableListableBeanFactory) it; + } + + if (it instanceof ConfigurableApplicationContext) { + return ((ConfigurableApplicationContext) it).getBeanFactory(); + } + + return null; + }); + for (Class type : typesToRegister) { - cacheFirstOrPrimary(type, repositoryFactoryInformation, BeanFactoryUtils.transformedBeanName(name)); + cacheFirstOrPrimary(beanFactory, type, repositoryFactoryInformation, BeanFactoryUtils.transformedBeanName(name)); } } @@ -273,6 +286,7 @@ public class Repositories implements Iterable> { return getRepositoryFactoryInfoFor(domainClass).getQueryMethods(); } + @Override public Iterator> iterator() { return repositoryFactoryInfos.keySet().iterator(); } @@ -281,29 +295,18 @@ public class Repositories implements Iterable> { * Caches the repository information for the given domain type or overrides existing information in case the bean name * points to a primary bean definition. * + * @param beanFactory must not be {@literal null}. * @param type must not be {@literal null}. * @param information must not be {@literal null}. * @param name must not be {@literal null}. */ @SuppressWarnings({ "rawtypes", "unchecked" }) - private void cacheFirstOrPrimary(Class type, RepositoryFactoryInformation information, String name) { + private void cacheFirstOrPrimary(Optional beanFactory, Class type, + RepositoryFactoryInformation information, String name) { if (repositoryBeanNames.containsKey(type)) { - 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.getMergedBeanDefinition(name)) // + Boolean presentAndPrimary = beanFactory.map(it -> it.getMergedBeanDefinition(name)) // .map(BeanDefinition::isPrimary) // .orElse(false);